Jump to content

Display data from mysql database from select drop down


FantomOptik

Recommended Posts

I am working on an inventory page. I would like to have a select drop down that lists each stone type (Marble, Travertine, Slate, etc.). My current code only retrieves the first record. When the user selects a stone from the drop down, I would like it to display all the records for that stone.Here is my code so far:In the 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","getinv.php?q="+str,true);xmlhttp.send();}</script>

Code for the select box

<form><select name="stones" onchange="showUser(this.value)"><option value="">Select a Stone:</option><option value="1">Marble</option><option value="2">Travertine</option><option value="3">Slate</option><option value="4">Onyx</option></select></form><br /><div id="txtHint"><b>View current inventory here.</b></div>

and finally my getinv.php

<?php$q=$_GET["q"];$con = mysql_connect('localhost', 'username', 'password');if (!$con)  {  die('Could not connect: ' . mysql_error());  }mysql_select_db("Inventory", $con);$sql="SELECT * FROM Products WHERE id = '".$q."'";$result = mysql_query($sql);echo "<table border='1'><tr><th>Stone Name</th><th>Stone Size</th></tr>";while($row = mysql_fetch_array($result))  {  echo "<tr>";  echo "<td>" . $row['StoneName'] . "</td>";  echo "<td>" . $row['StoneSize'] . "</td>";  echo "</tr>";  }echo "</table>";mysql_close($con);?>

I would like it to display all of the records where StoneType=Marble, etc.Thanks in advance for any advice!

Link to comment
Share on other sites

<select name="stones" onchange="showUser(this.value)">

check here: http://www.w3schools.com/jsref/dom_obj_select.aspto get the number (1, 2, 3 etc) you can use this.options[this.selectedIndex].value

$sql="SELECT * FROM Products WHERE id = '".$q."'";

if the id field is a number type in the sql table, you don't need the apostrophes around the value.

$q=$_GET["q"];

add (int) after the = sign, to stop sql injection.

Link to comment
Share on other sites

See, that is why I love this forum. I have learned so much from the tuts and from the great people here.Thanks for the help. I will give it a try!

<select name="stones" onchange="showUser(this.value)">

check here: http://www.w3schools.com/jsref/dom_obj_select.aspto get the number (1, 2, 3 etc) you can use this.options[this.selectedIndex].value

$sql="SELECT * FROM Products WHERE id = '".$q."'";

if the id field is a number type in the sql table, you don't need the apostrophes around the value.

$q=$_GET["q"];

add (int) after the = sign, to stop sql injection.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...