anthony anthony - 2 years ago 65
MySQL Question

Can't get selected value from dropdown in PHP, SQL

I have the following code:

<form action="MovieDatabase.php" method='post'>
<select name="director_movie">

$sql = mysqli_query($mysql, "SELECT DISTINCT Title FROM Film2");

while ($row = $sql->fetch_assoc()){

<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>

// close while loop

For the drop-down, which uses the values from SQL table. So the drop-down has values as movie titles.

When I try to access the selected value from MovieDatabase.php like that:


and when I try to echo it, it only shows me option value direcotr_movie1 which is not what I'm looking for. In this case I want to get the title of the movie, which was selected from the dropdown.

What am I doing wrong?

Answer Source

You hard-coded same value for each option in your code in this line:-

<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>

That's why you are getting same-value every time.

Change it to below:-

<option value="<?php echo $row['Title'];?>"><?php echo $row['Title'];?></option>

Now it will work fine.

Note:- id need to be unique for each HTML element so remove that repetitive id (i don't think you have any need of that)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download