473,800 Members | 2,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

'PHP and AJAX MySQL Database Example'

jafarsalam
1 New Member
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.c om/php/php_ajax_databa se.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
6 9758
ak1dnar
1,584 Recognized Expert Top Contributor
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
bluez
5 New Member
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="showU ser(this.value) ">
<option value="1">20000 01</option>
<option value="2">20000 02</option>
<option value="3">20000 03</option>
<option value="4">20000 04</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
1,584 Recognized Expert Top Contributor
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="showU ser(this.value) ">
<option value="1">20000 01</option>
<option value="2">20000 02</option>
<option value="3">20000 03</option>
<option value="4">20000 04</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
bluez
5 New Member
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
1,584 Recognized Expert Top Contributor
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
bluez
5 New Member
Thanks ajaxrand...

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

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

Similar topics

3
7490
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work on her local machine. Everything looks pretty good. OpenLDAP/cygwin works great. PostgreSQL works great. Apache runs. PHP runs. But when I try to connect to my PostgreSQL server using PHPPgAdmin, I
7
3773
by: Ivan Marsh | last post by:
Hey Folks, I'm having a heck of a time wrapping mind around AJAX. Anyone know of a simple, straight-forward example for pulling a simple query from mysql with PHP using AJAX? As I understand it I need a PHP script that pulls the query and dumps the data into XML format, that is called by triggering a javascript event that reads that file with XMLhttprequest.
4
3771
by: d3vkit | last post by:
Okay so I am at a loss here. I have a website that I've previously had no trouble connecting to the mysql DB on. I have an include to a connect file with the relevant connection info, and it was working fine until today. I am trying to implement some ajax with the javascript framework mootools (although I don't see how this is causing the problem it started happening right around this time sooo...) I am sending info from my login form to the...
0
1822
by: minnie | last post by:
An AJAX Simple Example for PHP Article from http://www.joyistar.com Introduction: AJAX WebShop 3 Beta2 supports PHP programming by integrating PHP5 development environment. Here we will give an simple example to show you how to develop web applications by PHP in AJAX WebShop. 1, Notice We will use Access database in this example. Of course, you can use others you like. Database name: demo.mdb; Table name: product; Fields:
1
2686
by: wradyn | last post by:
As an example, my MySQL database has 3 tables: main-area, sub-area, and items. Using PHP I populate my first select-list as follows: <?php // Create list containing the Main Areas echo "Select an Area:"; echo "<select name='area_select' id='area_select' onChange='getArea();'>\n"; while ($row = mysqli_fetch_assoc($result)) { extract($row); echo "<option value=\'$area_id\'>$name\n";
6
1950
by: John Doe | last post by:
Here's my issue: I have an instant messenger type feature for my site, its basically an ajax IM feature. I run with a mysql backend on the site, i have a div on my main page that runs a javascript timer to load the php page via ajax every 15 seconds which checks the "pinguser" table to see if the user has any chat requests, then echoes them to a div on the page. what i REALLY want to do is have the ajax page see if there is a ping for the...
9
1783
by: art | last post by:
Hi, We have some scripts here where we use some AJAX to populate some of the page. Basically the AJAX routine calls a PHP script. That PHP script uses a bunch of ECHO statements to create the page. Then, we use the innerHTML to populate the section on the page. The problem is, we have a small javascript routine in the middile of
1
1507
by: wheatcom | last post by:
This problem relates to modifying the code in the PHP and AJAX MySql Database Example in http://bytes.com/forum/thread608395.html. In that example, the value selected in the HTML form is from a drop down menu: <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>
2
2858
by: ArizonaJohn | last post by:
Hello, Below I have some Ajax and the page it points to. This code works great if $_SESSION has no spaces in it (for example, if it is "elpaso"). However, if $_SESSION has a space in it (for example, "el paso"), then the Ajax fails. What can I do to make this Ajax work when $_SESSION has a space in it? Thanks. Ajax: <?php
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10035
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.