简体   繁体   English

在PHP中获取列表框的值

[英]get value of listbox in php

I have the following code: 我有以下代码:

<td>Province</td>
<td>
<select name="prov">
<?php 
$sql1 = mysqli_query($connect, "SELECT * FROM Provincetbl order by Prov_Desc desc");
while ($row1 = $sql1->fetch_assoc()){
?>
<option value="prov1"><?php echo $row1['Prov_Desc']; ?> </option>
<?php 
}
?>
</select><br><br></td></tr>
<td>Distrect</td>
<td>
<select name="dist">
<?php 
$sql2 = mysqli_query($connect, "SELECT * FROM Distrecttbl order by Distrect_Desc desc");
while ($row2 = $sql2->fetch_assoc()){
?>
<option value="dist1"><?php echo $row2['Distrect_Desc']; ?> </option>
<?php 
}
?></select></td></tr>

There are two listboxes prov and dist. 有两个列表框prov和dist。 I have been trying to figure out how to read the value of either listbox (ie get it in a variable). 我一直在试图找出如何读取任一列表框的值(即在变量中获取它)。 I have found a post which states that the value of a listbox is read after the form is posted (in the variable $_Post). 我发现一个帖子,指出在发布表单后(变量$ _Post中)读取了列表框的值。 However, I am dealing with a listbox which is not sending anything to the server, so why is the value of the listbox read after the form is posted? 但是,我正在处理一个列表框,该列表框未向服务器发送任何内容,所以为什么在发布表单后读取列表框的值? Second, I do not have a form element in my page to begin with..? 其次,我的页面开始没有表单元素。 Other posts have stated that I can read the value of listbox in a variable $prov or $dist. 其他帖子指出,我可以在$ prov或$ dist变量中读取listbox的值。 However I cannot seem to echo the contents of these variables to the screen. 但是我似乎无法在屏幕上回显这些变量的内容。

Thank you for your help. 谢谢您的帮助。

I think all the options in the prov listbox will have the same value that you've wrote 'prov1' and the second listbox will have dist1 in all the options 我认为prov列表框中的所有选项将具有与您编写的“ prov1”相同的值,第二个列表框中的所有选项将具有dist1
What I recommande you to do is the following : 我建议您采取以下措施:
<option value="$row1['Prov_Desc']"><?php echo $row1['Prov_Desc']; ?> </option>
and for the second one use this : 第二个使用这个:
<option value="$row2['Distrect_Desc']"><?php echo $row2['Distrect_Desc']; ?> </option>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM