php - Simple Ajax filter not returning results -
i trying implement simple filter of data stored in mysql database through drop-down menu using code w3 schools website (please bear in mind new javascript!). ajax script returns no results. appreciated.
ajax.html
<html> <head> <script> function showuser(str) { if (str == "") { document.getelementbyid("txthint").innerhtml = ""; return; } else { if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); } else { // code 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="+str,true); xmlhttp.send(); } } </script> </head> <body> <form> <select name="genre" onchange="showuser(this.value)"> <option value="">select genre:</option> <option value="1">clubbing</option> <option value="2">comedy</option> </select> </form> <br> <div id="txthint"><b>person info listed here...</b></div> </body> </html>
getuser.php
<!doctype html> <html> <head> <style> table { width: 100%; border-collapse: collapse; } table, td, th { border: 1px solid black; padding: 5px; } th {text-align: left;} </style> </head> <body> <script type = "text/javascript" src="ajax.html"></script> <?php $q = intval($_get['q']); $con = mysqli_connect('localhost','root','','python'); if (!$con) { die('could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"ajax"); $sql="select * info id = '".$q."'"; $result = mysqli_query($con,$sql); echo "<table> <tr> <th>venue</th> <th>date</th> <th>genre</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['venue'] . "</td>"; echo "<td>" . $row['datez'] . "</td>"; echo "<td>" . $row['genre'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?> </body> </html>
found solution, if stuck similar thing...
function ajaxfunction(){ var ajaxrequest; // variable makes ajax possible! try{ // opera 8.0+, firefox, safari ajaxrequest = new xmlhttprequest(); }catch (e){ // internet explorer browsers try{ ajaxrequest = new activexobject("msxml2.xmlhttp"); }catch (e) { try{ ajaxrequest = new activexobject("microsoft.xmlhttp"); }catch (e){ // went wrong alert("your browser broke!"); return false; } } } // create function receive data // sent server , update // div section in same page. ajaxrequest.onreadystatechange = function(){ if(ajaxrequest.readystate == 4){ var ajaxdisplay = document.getelementbyid('ajaxdiv'); ajaxdisplay.innerhtml = ajaxrequest.responsetext; } } // value user , pass // server script. var gen = document.getelementbyid('gen').value; var querystring = "?gen=" + gen ; ajaxrequest.open("get", "getuser.php" + querystring, true); ajaxrequest.send(null); }
Comments
Post a Comment