简体   繁体   English

如何获得从中选择的选项的价值?

[英]How do I obtain the value of the option that was chosen in a select from?

In the PHP code below I have created a form that is used to create a drop down list. 在下面的PHP代码中,我创建了一个用于创建下拉列表的表单。

<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
    unset($network_id, $network_name);
    $network_id = $network_name['network_id'];
    $network_name = $result1['network_code'];
    echo '<option name="entry" value="' . $network_id . '">' . $network_name . '</option>';
    $network_chosen = $network_id;

}

echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>

This code works as desired. 此代码按需工作。 It populates a drop down list based on the results of a database query. 它根据数据库查询的结果填充一个下拉列表。 After the form has been submitted, I want to obtain the option that was selected and use it in another query. 提交表单后,我要获取选择的选项并在另一个查询中使用它。 After the submit, I print the contents of the $_POST variable using: 提交后,我使用以下命令打印$ _POST变量的内容:

print_r($_POST);

and my results are as follow: Array ( [network] => [submit] => Send ) 我的结果如下:Array([network] => [submit] => Send)

I want to get the value that was selected for network but it appears to be blank. 我想获取为网络选择的值,但它似乎为空。 Can anyone tell me what I'm doing wrong. 谁能告诉我我在做什么错。

By the way, I am really new at coding and this is my first time using SO so please excuse any usage errors on my part. 顺便说一下,我真的是编码新手,这是我第一次使用SO,所以请原谅任何使用错误。 Thanks. 谢谢。

$network_id = $result1['network_id']; instead of $network_id = $network_name['network_id'] 而不是$network_id = $network_name['network_id']

Output like this: 像这样的输出:

Array ( [network] => 2 [submit] => Send )

You are setting a wrong value to network_id, that's why it's output is blank. 您为network_id设置了错误的值,这就是为什么它的输出为空白。 Also, the 'name' attribute on option tags is not needed. 另外,不需要选项标签上的“名称”属性。 Try the changes below. 请尝试以下更改。

<?php
echo "<body>";
echo "<div id='network_name' class='col-md-3'>";
echo "<h2> Agency Network </h2>";
echo "<form action='droplistpop.php' method='post'>";
echo "<select name='network'>";
while($result1 = mysqli_fetch_assoc($result)) {
    unset($network_id, $network_name);
    $network_id = $result1['network_id'];
    $network_name = $result1['network_code'];
    echo '<option value="' . $network_id . '">' . $network_name . '</option>';
    $network_chosen = $network_id;

}

echo "</select>";
echo "<input name='submit' type='submit' value='Send' />";
echo "</form>";
echo "</div>";
?>

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

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