Reputation: 143
I am trying to create a dropdown menu which has values from the database and when selected it should show all the information that are connected to the selected value.
In MySQL I have three tables, person, address and resume. I created two drop-down menu's(see code below) which are showing the values from address > address_state and address > address_city. What needs to happen is when I selected a state it will show only the persons living in the selected state. And when selecting city it has to show all the persons living in the same city.
My drop-down menu select state only:(got the same code city)
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// CREATE A CONNECTION WITH THE DATABASE
// CONNECTIE MAKEN MET DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$sql="SELECT name,id FROM student";
$sql="SELECT DISTINCT address_state FROM address ORDER BY address_state asc";
/* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
echo "<select name=address value=''>Student Name</option>"; // list box select command
foreach ($conn->query($sql) as $row){//Array or records stored in $row
echo "<option value=>$row[address_state]</option>";
/* Option values are added by looping through the array */
}
echo "</select>";// Closing of list box
?>
I know you cannot provide any code because I don't have any code to show which actually "selects" the persons. But I have searched on Google for tutorials and I did not find one which did the thing I needed. So I hope you guys can help me or can provide any tutorials.
Upvotes: 0
Views: 97
Reputation: 1263
First replace:
echo "<select name=address value=''>Student Name</option>";
To:
echo '<select name="address">';
Next replace this:
echo "<option value=>$row[address_state]</option>";
With:
echo '<option value="'.$row[address_state].'">'.$row[address_state].'</option>';
Upvotes: 1
Reputation: 2059
Replace this line:
echo "<option value=>$row[address_state]</option>";
With
echo "<option value='".$row['address_state']."'>".$row['address_state']."</option>";
Upvotes: 4