5 Replies - 1522 Views - Last Post: 08 April 2015 - 10:54 AM Rate Topic: -----

#1 helloearth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 15

Chat Box with PHP JavaScript and AJAX

Posted 08 April 2015 - 12:50 AM

I have two php files, one to handle the page layout: index.php and another to handle to handle anything database related: insert.php

The following does not update as I expect it to.

<div id="chatlogs">
loading chatlogs please wait...
</div>



It produces undefined, yet when I debug it in Chrome, and check preview you can see that it's updating the database with what I expect.. it's just that in insert.php

echo $extract["username"] . ": " . $extract["msg"] . "<br>";


doesnt seem to display it, instead only show's undefined.

index.php
<html>
<head>
<title>chat box</title>

<script type="text/javascript">
function submitChat(){
		if(form1.uname.value == '' || form1.msg.value == ''){
			alert('all fields are mandatory');
			return;
		}
		var uname = form1.uname.value;
		var msg = form1.msg.value;
		var xmlhttp = new XMLHttpRequest();
		
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				document.getElementById('chatlogs').innerHTML = xmlhttp.respondText;
			}
		}
		xmlhttp.open('GET', 'insert.php?uname='+uname+'&msg='+msg, true);
		xmlhttp.send();
}
</script>
</head>

<body>
<form name="form1">
Enter you ChatName: <input type = "text" name ="uname" /><br/>
Your Message: <br/>

<textarea name ="msg"></textarea><br />
<a href="#" onclick="submitChat()">Send</a><br /><br />

<div id="chatlogs">
loading chatlogs please wait...
</div>

</body>


insert.php
<?php
/*two php variables to hold the query string data from index.php*/
$uname = $_REQUEST["uname"];
$msg = $_REQUEST["msg"];

/*no password for mysql*/
$con = mysql_connect("localhost", "root", "password")or die(mysql_error());

/*select the database*/
mysql_select_db("chatbox",$con)or die(mysql_error());
$uname = strip_tags(stripslashes(mysql_real_escape_string($uname)));
$msg = strip_tags(stripslashes(mysql_real_escape_string($msg)));

/*query to insert the data to the database*/

mysql_query("INSERT INTO `chatbox`.`logs` (`username` , `msg`) VALUES ('$uname','$msg')")or die(mysql_error());

/*create another query to display/fetch all the logs*/
$result1 = mysql_query("SELECT * FROM `logs` ORDER by `id` DESC")or die(mysql_error());
while($extract = mysql_fetch_assoc($result1)){
	echo $extract["username"] . ": " . $extract["msg"] . "<br>";
}
?>




Is This A Good Question/Topic? 0
  • +

Replies To: Chat Box with PHP JavaScript and AJAX

#2 andrewsw  Icon User is online

  • the case is sol-ved
  • member icon

Reputation: 6379
  • View blog
  • Posts: 25,774
  • Joined: 12-December 12

Re: Chat Box with PHP JavaScript and AJAX

Posted 08 April 2015 - 02:46 AM

It should be responseText not respondText.

The mysql extension is deprecated and should not be used for new code. See my signature below.



You should be checking your browser's console for any errors. There are links for the various browsers in my signature.

This post has been edited by andrewsw: 08 April 2015 - 02:49 AM

Was This Post Helpful? 1
  • +
  • -

#3 helloearth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 15

Re: Chat Box with PHP JavaScript and AJAX

Posted 08 April 2015 - 07:51 AM

thank you

can't believe i didn't spot that small error.

I've just updated it to mysqli.

thank you
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 13,474
  • Joined: 08-August 08

Re: Chat Box with PHP JavaScript and AJAX

Posted 08 April 2015 - 08:31 AM

You should post your updated code. Simply changing to MySQLi is not enough, you need to use prepared statements, and since this is your first attempt it would be wise to have others check your work.
Was This Post Helpful? 1
  • +
  • -

#5 helloearth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 15

Re: Chat Box with PHP JavaScript and AJAX

Posted 08 April 2015 - 09:16 AM

updated insert.php

<?php
/*two php variables to hold the query string data from index.php*/
$uname = $_REQUEST["uname"];
$msg = $_REQUEST["msg"];

/*no password for mysql*/
$con = mysqli_connect("localhost", "root", "password");

/*select the database*/
mysqli_select_db($con, "chatbox")or die(mysqli_error());
$uname = strip_tags(stripslashes(mysqli_real_escape_string($con, $uname)));
$msg = strip_tags(stripslashes(mysqli_real_escape_string($con, $msg)));

/*query to insert the data to the database*/

mysqli_query($con,"INSERT INTO `chatbox`.`logs` (`username` , `msg`) VALUES ('$uname','$msg')")or die(mysqli_error());

/*create another query to display/fetch all the logs*/
$result1 = mysqli_query($con, "SELECT * FROM `logs` ORDER by `id` DESC")or die(mysqli_error());
while($extract = mysqli_fetch_assoc($result1)){
	echo $extract["username"] . ": " . $extract["msg"] . "<br>";
}
?>



Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • View blog
  • Posts: 13,474
  • Joined: 08-August 08

Re: Chat Box with PHP JavaScript and AJAX

Posted 08 April 2015 - 10:54 AM

http://php.net/manua...-statements.php
As I'd thought, you're still using data directly in a query. Read the link above and try again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1