hackerkts Posted December 20, 2009 Share Posted December 20, 2009 Hi guys, I'm quite new to ajax, so pardon me. I have a html page where user will type in some unique number and it will search through MySQL and show their name. Beside their name there will be a checkbox for them to tick. Here comes the problem, I'm trying to update my MySQL once user tick beside their name, ajax doesn't seems to work. index.html <html> <head> <script src="clienthint.js"></script> </head> <body> <form> Admin No: <input type="text" onKeyUp="showHint(this.value)" /> </form> <p>Suggestions:</p> <p><span id="txtHint"></span></p> </body> </html> clienthint.js var xmlhttp function showHint(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support XMLHTTP!"); return; } var url="gethint.php"; url=url+"?q="+str; //url=url+"&sid="+Math.random(); xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function chkit(str) { if (str.length==0) { document.getElementById("txtHint").innerHTML=""; return; } xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Your browser does not support XMLHTTP!"); return; } var url=url+"?admin="+str; xmlhttp.onreadystatechange=stateChanged; xmlhttp.open("GET",url,true); xmlhttp.send(null); } function stateChanged() { if (xmlhttp.readyState==4) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } function GetXmlHttpObject() { if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } gethint.php <?php echo $_SERVER['REQUEST_URI']; $mysql = mysql_connect("localhost", "root", null); mysql_select_db("openhouse", $mysql); $query = mysql_query("SELECT name,admin FROM attendance WHERE training='no' ORDER BY name")or die("MySQL Error: ".mysql_error()); while($r = mysql_fetch_assoc($query)) { $a[] = $r['admin']; $b[] = $r['name']; } //get the q parameter from URL $q=$_GET["q"]; //lookup all hints from array if length of q>0 echo '<table border="1">'; if (strlen($q) > 0) { $hint=""; for($i=0; $i<count($a); $i++) { if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))) { if ($hint=="") { //$hint=$b[$i]; echo '<tr>'; echo '<td>'.$b[$i].'</td>'; echo '<td><input name="chk" type="checkbox" value="'.$a[$i].'" onClick="chkit(this.value)" /></td>'; echo '</tr>'; } else { $hint=$hint." <br> ".$b[$i]; } } } } echo '</table>'; // Set output to "no suggestion" if no hint were found // or to the correct values if ($hint == "") { $response="no suggestion"; } else { //$response=$hint; $response = null; } //output the response echo $response; ?> Quote Link to comment Share on other sites More sharing options...
hackerkts Posted December 20, 2009 Author Share Posted December 20, 2009 Sorry to add on, I would like the link to add a?=NUMBER when I check on the checkbox, the number is the value of the check form. Quote Link to comment Share on other sites More sharing options...
gevensen Posted December 25, 2009 Share Posted December 25, 2009 i found this works http://www.phpfreaks.com/forums/index.php/topic,257587.msg1211752.html#msg1211752 Quote Link to comment Share on other sites More sharing options...
gevensen Posted December 31, 2009 Share Posted December 31, 2009 del wrong thread Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.