php - retrieve data from database mysql not working -
hi using html dropdown's onchange event using ajax
in code using, should address column value when change drop down.
but not working.what may have gone wrong?
here code
<html> <head> <script> function showuser( str ) { if ( str == "" ) { document.getelementbyid("txthint").innerhtml=""; return; } 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> <?php mysql_connect('localhost', 'tiger', 'tiger'); mysql_select_db('theaterdb'); $sql = "select theater_name theater;"; $result = mysql_query($sql); echo "<select name='theater_name' id='course' onchange='showuser(this.value);'>"; while ( $row = mysql_fetch_array( $result ) ) { echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>"; } echo "</select>"; ?> </form> <br> <div id="txthint"><b>info</b></div> </body> </html>
code getuser.php
<?php $q = $_get["q"]; $con = mysqli_connect("localhost", "tiger", "tiger", "theaterdb"); if ( !$con ) { die('could not connect: ' . mysqli_error( $con ) ); } mysqli_select_db( $con ); $sql = "select address theater theater_name = '".$q."'"; $result = mysqli_query( $con, $sql ); echo "<table border='1'> <tr> <th>firstname</th> </tr>"; while( $row = mysqli_fetch_array( $result ) ) { echo "<tr>"; echo "<td>" . $row['address'] . "</td>"; echo "</tr>"; } echo "</table>"; mysqli_close($con); ?>
ok, i've tweaked files slightly, shouldn't using mysql_ or mysqli_ functions more, don't... , shouldn't using mysql function in 1 file , mysqli functions in other... i've switched them on use pdo, you're script isn't susceptible sql injection, , far can tell works fine.
index.html
<html> <head> <script> function showuser(str) { if(str=="") { document.getelementbyid("txthint").innerhtml=""; return; } if(window.xmlhttprequest) { xmlhttp=new xmlhttprequest(); } else { 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> <?php try { $dbh = new pdo('mysql:dbname=theaterdb;host=localhost','tiger','tiger'); } catch (pdoexception $e) { echo 'connection failed: ' . $e->getmessage(); } $sql = "select theater_name theater;"; $sth = $dbh->prepare($sql); $sth->execute(); echo "<select name='theater_name' id='course' onchange='showuser(this.value);'>"; while($row = $sth->fetch(pdo::fetch_assoc)) { echo "<option value='" . $row['theater_name'] ."'>" . $row['theater_name']. "</option>"; } echo "</select>"; ?> </form> <br> <div id="txthint"><b>info</b></div> </body> </html>
getuser.php
<?php $q = strtolower(trim($_get["q"])); try { $dbh = new pdo('mysql:dbname=theaterdb;host=localhost','tiger','tiger'); } catch (pdoexception $e) { echo 'connection failed: ' . $e->getmessage(); } $sql = 'select address theater lower(theater_name) = :q'; $sth = $dbh->prepare($sql); $sth->bindvalue(':q', $q); $sth->execute(); echo "<table border='1'><tr><th>firstname</th></tr>"; while($row = $sth->fetch(pdo::fetch_assoc)) { echo "<tr>"; echo "<td>" . $row['address'] . "</td>"; echo "</tr>"; } echo "</table>"; $dbh = null;
Comments
Post a Comment