Jump to content

Php And Ajax Mysql Database Example Question


rakelbara

Recommended Posts

Hi I've been looking at the http://www.w3schools...ax_database.asp tutorial and trying to get it to work for my needs.But my problem is that I want the dropdown menu to be generated from a mysql database and the names have two different fields in my table (firstname, lastname). But my problem is, that it seems to break the connection to the ajax function because I don't get anything listed below the dropdown upon selection. See example here http://www.hafdal.dk/testing/users.php My code is: <html><head> <?php require_once 'login.php';$db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database, $db_server)or die("Unable to select database: " . mysql_error()); $results=mysql_query("SELECT * FROM user") or die(mysql_error()); echo '<select name="myselect" onselect="showUser();return false;">';while($row=mysql_fetch_assoc($results)) { echo "<option value=\"{$row['id']}\">{$row['firstname']} {$row['lastname']}</a></option>";} echo "</select>"; ?> <script type="text/javascript">function showUser(str){var element = document.getElementById("myselect");var evalue = element.options[element.selectedIndex].value; 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; } }xmlhttp.open("GET","getuser.php?q="+evalue,true);xmlhttp.send();}</script></head><body> <br /><div id="txtHint"><b>Person info will be listed here.</b></div> </body></html>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...