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

Ajax not working with form

P: 6
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
Share this Question
Share on Google+
4 Replies


dmjpro
100+
P: 2,476
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
Expert Mod 15k+
P: 16,027
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
100+
P: 2,476
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
Expert Mod 15k+
P: 16,027
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

Post your reply

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