473,320 Members | 1,993 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Ajax not working with form

I am not sure why this code is not working. I've verified the PHP works by viewing that page and testing it by hardcoding values to activate the query.

Nothang happens when I select a choice on the form. Javascript and ajax works for other pages. I'm a newbie so I know there is a stupid mistake here


Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script src="selectuser.js"></script>
  4. </head>
  5. <body><form> 
  6. Select a User:
  7. <select name="users" onchange="showUser(this.value)">
  8. <option value="1">Peter Griffin</option>
  9. <option value="2">Ronald Reagan</option>
  10. <option value="3">Harry Potter</option>
  11. </select>
  12. </form><p>
  13. <div id="txtHint"><b>User info will be listed here.</b></div>
  14. </p></body>
  15. </html>



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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $q=$_GET["q"];
  3.  
  4. $con = mysql_connect('localhost', 'user id', 'password');
  5. if (!$con)
  6.  {
  7.  die('Could not connect: ' . mysql_error());
  8.  }
  9.  
  10. mysql_select_db("test_database", $con);
  11. //$q = 2;
  12.  
  13. $sql="SELECT * FROM user WHERE id = '".$q."'";
  14.  
  15. $result = mysql_query($sql);
  16.  
  17. echo "<table border='1'>
  18. <tr>
  19. <th>Firstname</th>
  20. <th>Lastname</th>
  21. <th>Age</th>
  22. <th>Hometown</th>
  23. <th>Job</th>
  24. </tr>";
  25.  
  26. while($row = mysql_fetch_array($result))
  27.  {
  28.  echo "<tr>";
  29.  echo "<td>" . $row['FirstName'] . "</td>";
  30.  echo "<td>" . $row['LastName'] . "</td>";
  31.  echo "<td>" . $row['Age'] . "</td>";
  32.  echo "<td>" . $row['Hometown'] . "</td>";
  33.  echo "<td>" . $row['Job'] . "</td>";
  34.  echo "</tr>";
  35.  }
  36. echo "</table>";
  37.  
  38. mysql_close($con);
  39. ?>
Nov 1 '07 #1
4 1173
dmjpro
2,476 2GB
I think you are getting the first alert.

Now make a changes in ..... function stateChanged

Expand|Select|Wrap|Line Numbers
  1. function stateChanged()
  2. {
  3.  alert(xmlHttp.readyState+"\t"+xmlHttp.status);
  4. }
  5.  
Now see what values come here....

Debasis
Nov 2 '07 #2
acoder
16,027 Expert Mod 8TB
I am not sure why this code is not working. I've verified the PHP works by viewing that page and testing it by hardcoding values to activate the query.

Nothang happens when I select a choice on the form. Javascript and ajax works for other pages. I'm a newbie so I know there is a stupid mistake here
Get rid of the return statement on line 5.
Nov 2 '07 #3
dmjpro
2,476 2GB
Get rid of the return statement on line 5.
That means there is wrong with Ajax Request Object making.
But i checked that code it worked for IE and FF.
Please explain Acoder.

Debasis
Nov 2 '07 #4
acoder
16,027 Expert Mod 8TB
If you have a return on line 5, the code gets the XMLHttp object, displays an alert and returns. The rest of the function does not execute.
Nov 2 '07 #5

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

Similar topics

3
by: noballack | last post by:
I've got a problem, I'm working with Ajax in a web with a form with a list of checkbox added to the form via an Ajax.Updater method. These added checkboxs are not been sended by the form if I use...
25
by: meltedown | last post by:
This is supposed ot be an example: http://www.ajaxtutorial.net/index.php/2006/11/30/simple-ajax-using-prototype-part-2/ It says : This example is probably the simplest example you will ever...
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
2
by: =?Utf-8?B?VG9u?= | last post by:
Hello, I want to understand teh benefits of ajax technology. Does anyone has a good website where AJAX EXTENSIONS is worked out so I really understand it. There a 2 main questions: 1) How about...
0
by: Tarik Monem | last post by:
I have been working on an all AJAX/DOM web site which is set to go live today and I thought I'd share my discoveries with all of you whom have helped me when I have encountered different issues along...
2
by: shivendravikramsingh | last post by:
hi friends, i m using a ajax function for retrieving some values from a database table,and display the values in required field,my prob is that the ajax function i m using is working f9 once,but if...
1
by: Mark B | last post by:
This is my first try at using AJAX. I want the calendars to be enabled if the user checks CheckBox1. It works OK for a normal all page refresh but once I introduced the AJAX code it stopped...
4
by: Peter | last post by:
ASP.NET 2.0 I have an AutoCompleteExtender which works fine- I am using name, id pair in the WebService , but what I am trying to do is: once the user selects an item from the...
1
by: javediq143 | last post by:
Hi All, This is my first post in this forum. I'm developing a CMS for my latest website. This CMS is also in PhP & MySQL. I'm done with the ADD section where the Admin can INSERT new records in...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.