[英]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">
<?php
$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>
<?php
// close while loop
}
?>
</form>
For the drop-down, which uses the values from SQL table. 对于下拉列表,它使用SQL表中的值。 So the drop-down has values as movie titles.
因此,下拉菜单具有作为电影标题的值。
When I try to access the selected value from MovieDatabase.php like that: 当我尝试像这样从MovieDatabase.php访问选定的值时:
$_POST['director_movie'];
and when I try to echo it, it only shows me option value direcotr_movie1 which is not what I'm looking for. 当我尝试回显它时,它仅显示选项值direcotr_movie1 ,这不是我想要的。 In this case I want to get the title of the movie, which was selected from the dropdown.
在这种情况下,我想获取电影的标题,该标题是从下拉列表中选择的。
What am I doing wrong? 我究竟做错了什么?
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) 注意:-每个HTML元素的
id
必须唯一,因此请删除该重复id
(我认为您不需要)
Change this 改变这个
<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>
to 至
<option value=<?php echo $row['Title']; ?> id="film_id"><?php echo $row['Title']; ?></option>
you are hardcoding your value <option value="director_movie1" id="film_id">
and wondering why your form submits your hardcoded value? 您正在对值
<option value="director_movie1" id="film_id">
硬编码,并且想知道为什么您的表单提交了硬编码的值?
The movie title that is shown in your select is just what the users can see. 您所选择的电影标题就是用户可以看到的。 your form submits what you put into
value=<your required information here>
您的表单将提交您投入
value=<your required information here>
On another note: Using the title as the field might result in problems when using anything but chars / numbers as the title when you want to fetch the movie information based on the selected title. 另外请注意:如果要根据所选标题获取电影信息,则在将字符(数字)用作标题时,将标题用作字段可能会导致问题。 Better use the ID of your movie
更好地使用电影的ID
1st : Change your value attribute to value="<?php echo $row['Title']; ?>"
1st:将您的value属性更改为
value="<?php echo $row['Title']; ?>"
2nd : Id attribute is unique for each element. 第二: Id属性对于每个元素都是唯一的。 remove it from option tag
id="film_id"
从选项标签
id="film_id"
其删除
<option value="<?php echo $row['Title']; ?>" ><?php echo $row['Title']; ?></option>
change below line 在行下更改
<option value="director_movie1" id="film_id"><?php echo $row['Title']; ?></option>
with 与
<option value="<?php echo $row['Title']; ?>" ><?php echo $row['Title']; ?></option>
Now you will get dynamic value different options also id
can not have same value for all 现在您将获得动态值不同的选项,而且
id
不能对所有
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.