vivek2tech Posted November 15, 2013 Share Posted November 15, 2013 When i am using this in my code and run getuser.php file the i am getting a drop down, a default table and a notice that is Undefined index: q in C:\wamp\www\sample\getuser.php on line 43 and when i select any name from drop down then there is making one more same drop down,and a new table which is working properly. But Problem is that what about the previous dropdown, table and notice. <html><head> <script type="text/javascript"> function showUser(str){ 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=" + str, true); xmlhttp.send(); } </script></head><body> <form> <select name="users" onchange="showUser(this.value)"> <option value="">Select a person:</option> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Glenn Quagmire</option> <option value="4">Joseph Swanson</option> </select> </form> <br/> <div id="txtHint"> <b>Person info will be listed here.</b> </div></body></html><?php$q=$_GET["q"]; mysql_connect('localhost', 'root', ''); mysql_select_db("sample"); $sql="SELECT * FROM user WHERE id = '".$q."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close(); ?> Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted November 15, 2013 Share Posted November 15, 2013 nd a notice that is Undefined index: q in C:\wamp\www\sample\getuser.php on line 43 You are getting the notice because the $_GET['q'] variable doesn't exist until the ajax request. You need to check that q exists in the $_GET superglobal before you use it. But Problem is that what about the previous dropdown, table and notice. Is that code all from getuser.php? If it is you need to isolate the code that should respond to the ajax request, for example you only want the users details (the table), Not the whole webpage for getusers.php. Otherwise your webpage will duplicate. The objective is to allow someone to select a user from your dropdown. What will happen is someone will select a user. The javascript code will then perform an ajax request to getuser.php and set the q query string variable to the selected users id. The PHP code will then fetch the users details that matches the id value in $_GET['q']. It'll then output the users details in a table. What is outout from getusers.php is used as the response. The response from the ajax request will be contained in the xmlhttp.responseText variable. You then add the response to the <div id="txtHint"></div> This is how you would setup your code. <?php // check that $_GET["q"] exists (ajax request has started) if(isset($_GET["q"])) { mysql_connect('localhost', 'root', ''); mysql_select_db("sample"); $q = mysql_real_escape_string($_GET['q']); $sql= "SELECT * FROM user WHERE id = '".$q."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close(); exit; //We only need to echo out the data we need for the request (this will be the response) } ?> <html> <head> <script type="text/javascript"> function showUser(str){ 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; /* the response */ } } xmlhttp.open("GET", "getuser.php?q=" + str, true); xmlhttp.send(); } </script> </head> <body> <form> <select name="users" onchange="showUser(this.value)"> <option value="">Select a person:</option> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Glenn Quagmire</option> <option value="4">Joseph Swanson</option> </select> </form> <br/> <div id="txtHint"> <b>Person info will be listed here.</b> </div> </body> </html> Quote Link to comment Share on other sites More sharing options...
brandon66 Posted November 21, 2013 Share Posted November 21, 2013 (edited) you should look into jquery ajax its fairly easy. heres an example. Other suggestions for you is to use PDO or MySQLi for your php <?php // check that $_GET["str"] exists (ajax request has started) if(isset($_GET["str"])) { mysql_connect('localhost', 'root', ''); mysql_select_db("sample"); $str = mysql_real_escape_string($_GET['str']); $sql= "SELECT * FROM user WHERE id = '".$str."'"; $result = mysql_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "<td>" . $row['Age'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close(); exit; //We only need to echo out the data we need for the request (this will be the response) } ?> <html> <head> <script src="//code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function(){ $('#users').change(function(){ var user = $('#users').val(); showUser(user); }); }); function showUser(str){ if (str === "") { //hide the div $('#txtHint').hide(); }else{ //send str to php and get back data $.post('getuser.php', {str: str}, function(data) { //output data into the empty div //this could also be your txthint div $('div#table').html(data); }); } } </script> </head> <body> <form> <select name="users" id="users"> <option value="">Select a person:</option> <option value="1">Peter Griffin</option> <option value="2">Lois Griffin</option> <option value="3">Glenn Quagmire</option> <option value="4">Joseph Swanson</option> </select> </form> <br/> <div id="txtHint"> <b>Person info will be listed here.</b> </div> <div id="table"></div> </body> </html> Edited November 21, 2013 by brandon66 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.