BrainBoxMad Posted July 1, 2013 Share Posted July 1, 2013 Hi people, i am basically trying to have the functionality of a dropdown menu that when chosen a specific value from it, the resulting display will be that values data from my databse table, this information gets displayed on a html format table. I am keep getting the error: Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 17 Please have in mind i am a completely novice with PHP, and doing this just to get better, please if someone can help me get my head around this, it will be much appreciated. Thank You. HTML CODE is: <html><head><script>function showproduct(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","getproduct.php?q="+str,true);xmlhttp.send();}</script></head><body><form><select name="laptop_products" onchange="showproduct(this.value)"><option value="">Select a person:</option><option value="1">SAMSUNG</option><option value="2">HP</option><option value="3">ASUS</option><option value="4">LENOVO</option></select></form><br><div id="txtHint"><b>Person info will be listed here.</b></div></body></html> The PHP CODE is: <?phpif (isset($_GET['q'])) {echo $_GET['q'];}$dbhandle = mysql_connect('localhost','root','','store');if (!$dbhandle) { die("Could not connect: " . mysql_error()); }mysql_select_db("store") or die ("Could not connect: " . mysql_error());$sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'";$result = mysql_query($sql);echo "<table border='1'><tr><th>product_id</th><th>product_name</th><th>weight</th><th>price</th><th>description</th></tr>";while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['product_id'] . "</td>"; echo "<td>" . $row['product_name'] . "</td>"; echo "<td>" . $row['weight'] . "</td>"; echo "<td>" . $row['price'] . "</td>"; echo "<td>" . $row['description'] . "</td>"; echo "</tr>"; }echo "</table>";mysql_close($dbhandle);?> Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/ Share on other sites More sharing options...
jcbones Posted July 2, 2013 Share Posted July 2, 2013 You haven't declared $q. You stopped at echo'ing $_GET['q'], but since registered_globals are turned off (as they should be), the $_GET array isn't extracted. if (isset($_GET['q'])) { echo $_GET['q']; $q = (int)$_GET['q']; } Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1438904 Share on other sites More sharing options...
BrainBoxMad Posted July 2, 2013 Author Share Posted July 2, 2013 You haven't declared $q. You stopped at echo'ing $_GET['q'], but since registered_globals are turned off (as they should be), the $_GET array isn't extracted. if (isset($_GET['q'])) { echo $_GET['q']; $q = (int)$_GET['q']; } Hey, i added the extra line of code but i am still for some reason still receiving the same error message :S I am sorry, but is there anything else another fresh pair of eyes my see what i am doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1439031 Share on other sites More sharing options...
ginerjm Posted July 2, 2013 Share Posted July 2, 2013 You could start by showing the new message and also the line that it points to Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1439035 Share on other sites More sharing options...
BrainBoxMad Posted July 2, 2013 Author Share Posted July 2, 2013 You could start by showing the new message and also the line that it points to Yes that is true sorry, well i am receiving the same error message Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 23 after using the advice that was offered above. The error is refering to line 23 which is: $sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'"; from the above code in the php page any hints? Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1439132 Share on other sites More sharing options...
Levr0x0rz Posted July 2, 2013 Share Posted July 2, 2013 Try this: $sql="SELECT * FROM laptop_products WHERE product_id = = '{$q}'"; JonAspiring Web Developer@JonEdney1 Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1439138 Share on other sites More sharing options...
BrainBoxMad Posted July 2, 2013 Author Share Posted July 2, 2013 Try this: $sql="SELECT * FROM laptop_products WHERE product_id = = '{$q}'"; Jon Aspiring Web Developer @JonEdney1 no seems not to work tried that before too, however, receive the following: Notice: Undefined variable: q in E:\website\htdocs\restricted\test\getproduct.php on line 24 Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in E:\website\htdocs\restricted\test\getproduct.php on line 37 Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1439142 Share on other sites More sharing options...
Levr0x0rz Posted July 2, 2013 Share Posted July 2, 2013 Change line 23 back to this: $sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'"; Them, where you have the variable q set, set it this way: $q = isset($_GET['q'])?$_GET['q']:null; Let me know if that gets things moving. JonAspiring Web Developer@JonEdney1 Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1439150 Share on other sites More sharing options...
BrainBoxMad Posted July 2, 2013 Author Share Posted July 2, 2013 Change line 23 back to this: $sql="SELECT * FROM laptop_products WHERE product_id = '".$q."'"; Them, where you have the variable q set, set it this way: $q = isset($_GET['q'])?$_GET['q']:null; Let me know if that gets things moving. Jon Aspiring Web Developer @JonEdney1 Jon, yeah, that worked, thank you,really appreciated, thanks for the time. However, now after resolving this problem, the main aim of this code which is to select a value from the drop down list and in return display the table related to that value from the database is not happening. Life story to PHP, never ending problems ; ) Quote Link to comment https://forums.phpfreaks.com/topic/279770-trying-to-make-data-from-database-table-display-on-webpage-when-user-chooses-appropriate-value-through-dropdown-list/#findComment-1439158 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.