Hi...
Im trying to connect AJAX with PHP/MYSQL. I wrote a basic code for connecting this. I got codings from http://www.w3schools.com/PHP/php_ajax_database.asp.
1) I created database and tables.
2) While i run the ajax_db.html, if i select anything, im getting
Unable to connect with the server:
[Object error]
I used document.write message and i checked the select name number is passing in the java script. But i doesnt call the xmlhttp.open(). I wrote the correct address. But im getting the error...
Will any one help me to correct this?
Below i list out the codings
ajax_db.html
ajax_db.js
getuser.php
Im trying to connect AJAX with PHP/MYSQL. I wrote a basic code for connecting this. I got codings from http://www.w3schools.com/PHP/php_ajax_database.asp.
1) I created database and tables.
2) While i run the ajax_db.html, if i select anything, im getting
Unable to connect with the server:
[Object error]
I used document.write message and i checked the select name number is passing in the java script. But i doesnt call the xmlhttp.open(). I wrote the correct address. But im getting the error...
Will any one help me to correct this?
Below i list out the codings
ajax_db.html
Code:
<html> <head> <title>Connecting with DB</title> <script type="text/javascript" src="ajax_db.js"> </script> </head> <body> <form> Select a person <select name="users" onchange="showUser(this.value)"> <option value="">Select a person:</option> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Glenn Quagmire</option> <option value="4">Joseph Swanson</option> </select> </form> <br /> <div id="txtHint"><b>Person info will be listed here.</b></div> </body> </html>
Code:
function showUser(str)
{
// document.write(str);
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
try{
//xmlhttp.open('GET','getuser.php?q='+str,true);
xmlhttp.open('GET','http://192.168.100.152/~rekha/AJAX/getuser.php?q='+str,true);
xmlhttp.send();
}catch(e) {
alert("Unable to connect with the server:\n" +e.toString());
}
}
getuser.php
Code:
<?php
$q = $_GET["q"];
echo $q;
$con = mysql_connect("192.168.100.152", "DB", "bbscn107");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("TEST", $con);
$sql="SELECT * FROM user WHERE id = '".$q."'";
echo $sql;
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Comment