By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,751 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,751 IT Pros & Developers. It's quick & easy.

'PHP and AJAX MySQL Database Example'

jafarsalam
P: 1
hi;

I'm new to PHP and AJAX MySQL codes. I found a simple code for
PHP and AJAX MySQL Database communication on :
http://www.w3schools.com/php/php_ajax_database.asp

I downloaded the code and uploaded to my server but it does not work . I script does not display the data from the database.

Can anyone look at this and help me out.

thanks
Feb 27 '07 #1
Share this Question
Share on Google+
6 Replies


ak1dnar
Expert 100+
P: 1,584
If you create the user table in ajax_demo database definitely it will work.

Why don't you post the Coding here, that you used in your server.
Feb 27 '07 #2

P: 5
Me too... I keep getting error message from the HTML page

Line : 2
Char : 21
Error : Expected ';'

Line : 7
Char : 1
Error : Object Expected

==============================================
[PHP]<html>
<head>
<script src="selectuser.js"></script>
</head>
<body><form>
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="1">2000001</option>
<option value="2">2000002</option>
<option value="3">2000003</option>
<option value="4">2000004</option>
</select>
</form><p>
<div id="txtHint"><b>User info will be listed here.</b></div>
</p></body>
</html>[/PHP]

Im really no idea
Thanks
Apr 10 '07 #3

ak1dnar
Expert 100+
P: 1,584
Me too... I keep getting error message from the HTML page

Line : 2
Char : 21
Error : Expected ';'

Line : 7
Char : 1
Error : Object Expected

==============================================
<html>
<head>
<script src="selectuser.js"></script>
</head>
<body><form>
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="1">2000001</option>
<option value="2">2000002</option>
<option value="3">2000003</option>
<option value="4">2000004</option>
</select>
</form><p>
<div id="txtHint"><b>User info will be listed here.</b></div>
</p></body>
</html>

Im really no idea
Thanks
This Error is Coming from your selectuser.js file. You are posting only HTML part.
Try this and if not working post the js file. Note the " ; " by the end of function.
Expand|Select|Wrap|Line Numbers
  1. <select name="users" onchange="showUser(this.value);">
Apr 11 '07 #4

P: 5
Here the JavaScript and PHP files. Im only did some changes on PHP file.

selectuser.js
Expand|Select|Wrap|Line Numbers
  1. var xmlHttpfunction showUser(str)
  2. xmlHttp=GetXmlHttpObject()
  3. if (xmlHttp==null)
  4.  {
  5.  alert ("Browser does not support HTTP Request")
  6.  return
  7.  } 
  8. var url="getuser.php"
  9. url=url+"?q="+str
  10. url=url+"&sid="+Math.random()
  11. xmlHttp.onreadystatechange=stateChanged 
  12. xmlHttp.open("GET",url,true)
  13. xmlHttp.send(null)
  14. }
  15.  
  16. function stateChanged() 
  17. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  18.  { 
  19.  document.getElementById("txtHint").innerHTML=xmlHttp.responseText
  20.  } 
  21. }function GetXmlHttpObject()
  22. {
  23. var xmlHttp=null;
  24. try
  25.  {
  26.  // Firefox, Opera 8.0+, Safari
  27.  xmlHttp=new XMLHttpRequest();
  28.  }
  29. catch (e)
  30.  {
  31.  //Internet Explorer
  32.  try
  33.   {
  34.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  35.   }
  36.  catch (e)
  37.   {
  38.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  39.   }
  40.  }
  41. return xmlHttp;
  42. }
  43.  
getuser.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. include("Database/database.php");
  4.  
  5. global $conn;
  6.  
  7. $q=$_GET["q"];
  8.  
  9. $sql="SELECT * FROM student_recording WHERE Stu_ID = $q";
  10.  
  11. $result = $conn->query($sql);
  12.  
  13. echo "<table border='1'>
  14. <tr>
  15. <th>Firstname</th>
  16. <th>Class Code</th>
  17. <th>Race</th>
  18. </tr>";
  19.  
  20. while($row = $result->fetch_assoc())
  21.  {
  22.  echo "<tr>";
  23.  echo "<td>" . $row['Stu_Name'] . "</td>";
  24.  echo "<td>" . $row['Class_Code'] . "</td>";
  25.  echo "<td>" . $row['Stu_Race'] . "</td>";
  26.  echo "</tr>";
  27.  }
  28. echo "</table>";
  29.  
  30. ?>
  31.  
Apr 11 '07 #5

ak1dnar
Expert 100+
P: 1,584
Here the JavaScript and PHP files. Im only did some changes on PHP file.
I think both of you are using This Sample Application.
jafarsalam,
If this is not your problem please post a comment on that.

bluez,
Try this Coding.Note that you have merged xmlHttp variable and the ShowUSer function in your JS file.
And the ; Missing by end of xmlHttp Variable Declaration.

Expand|Select|Wrap|Line Numbers
  1. var xmlHttp;
  2.  
  3. function showUser(str)
  4. xmlHttp=GetXmlHttpObject()
  5. if (xmlHttp==null)
  6.  {
  7.  alert ("Browser does not support HTTP Request")
  8.  return
  9.  } 
  10. var url="getuser.php"
  11. url=url+"?q="+str
  12. url=url+"&sid="+Math.random()
  13. xmlHttp.onreadystatechange=stateChanged 
  14. xmlHttp.open("GET",url,true)
  15. xmlHttp.send(null)
  16. }
  17.  
  18. function stateChanged() 
  19. if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
  20.  { 
  21.  document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
  22.  } 
  23. }
  24.  
  25. function GetXmlHttpObject()
  26. {
  27. var xmlHttp=null;
  28. try
  29.  {
  30.  // Firefox, Opera 8.0+, Safari
  31.  xmlHttp=new XMLHttpRequest();
  32.  }
  33. catch (e)
  34.  {
  35.  //Internet Explorer
  36.  try
  37.   {
  38.   xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  39.   }
  40.  catch (e)
  41.   {
  42.   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  43.   }
  44.  }
  45. return xmlHttp;
  46. }
  47.  
Apr 11 '07 #6

P: 5
Thanks ajaxrand...

It's work now. Sorry for never check it properly :)
Apr 12 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.