【问题标题】:How to pass the selected value in dropdown (HTML) to PHP如何将下拉列表(HTML)中的选定值传递给 PHP
【发布时间】:2015-11-02 21:19:52
【问题描述】:

我正在动态构建一个选择选项。我从选择选项中选择一个值。 (列表的值来自 php 如何将选定的值传递给 PHP?

<select id ="s1" name="swimopt" class="so">
    <?php echo $options; ?>
</select>' 

$options 来自 MySQL 并填充下拉列表

当我选择一个值,然后尝试

echo $_POST['swimopt'];

不显示所选值

请帮忙

 <form id="swimdata"  method="POST" action="save.php">

     <input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
     <input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender"  value="girls">GIRLS

     <table id="meetTable" style="width:auto">


    <tr>
        <th>EVENT:</th>
        <th>NAME:</th>
        <th>LANE:</th>
        <th>TIME:</th>
        <th>PLACE:</th>
        <th>SCORE 1:</th>
        <th>SCORE 2:</th>
        <th>PLACE:</th>
        <th>TIME:</th>
        <th>LANE:</th>
        <th>NAME:</th>

    </tr>

</table>

     <input type="submit" name="formSubmit" value="Submit" />
     <input type="hidden" name="action1" value="addSwimmer" id="action1">

 </form>

这是我的 PHP 从 mySQL 获取 $options

if ($result->num_rows > 0) {
    $options= '';
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $options .=  "<tr><td>" . $row["swimmer_id"]. "</td><td>" . $row["first_name"]. " " . $row["last_name"]. " </td><td> "  . $row["school_name"]. "</td></tr>";
    }

} else {
    echo "0 results";
}
echo $options;
$conn->close();

截图:

选项回显到选择框中:

<select id ="s1" name="swimopt" class="so">
    <?php echo $options; ?>
</select>' 

PHP文件代码

$sql = "select  first_name, last_name  from swimming where gender='m'";

$results = mysqli_query($link,$sql);

if ($results->num_rows > 0) {
    $options = '';
    // output data of each row
    while($row = $results->fetch_assoc()) {
        $fname=$row["first_name"];
        $lname=$row["last_name"];

        $options .= "<option value= >" . $row["first_name"] . "  " . $row["last_name"]."</option>";


    }
} else {
    echo "0 results";
}
echo $options;
$link->close();


?>

【问题讨论】:

  • 显示你的完整代码 ;) 用表单和你的 php 部分周围的东西。
  • 从我的 html 文件中添加了表单
  • 我没有看到名为“swimopt”的输入。您是否尝试过 print_r($_POST) 并查看通过了什么?
  • '
  • 选择驻留在表单中的什么位置?正如 Robbie 所说,当您提交表单时,在 PHP 端回显变量。使用print_r($_POST)var_dump($_POST)

标签: php html


【解决方案1】:

我想我明白你的问题是什么了。您没有设置选择选项。 试试这个:

<?php
$options = 'John';
?>

<form id="swimdata"  method="POST" action="save.php">

<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender" value="boys">BOYS
<input type="radio" style="font-size: 16px;font-weight: bolder" name="gender" class ="gender"  value="girls">GIRLS

<table id="meetTable" style="width:auto">


<tr>
<th>EVENT:</th>
<th>NAME:</th>
<th>LANE:</th>
<th>TIME:</th>
<th>PLACE:</th>
<th>SCORE 1:</th>
<th>SCORE 2:</th>
<th>PLACE:</th>
<th>TIME:</th>
<th>LANE:</th>
<th>NAME:</th>

</tr>

</table>
<select id ="s1" name="swimopt" class="so" value="">
<?php
for($i=0;$i<10;$i++){
echo '<option value="user '.$i.'" >user '.$i.'</option>';
}
?>
</select> 
<input type="submit" name="formSubmit" value="Submit" />
<input type="hidden" name="action1" value="addSwimmer" id="action1">

</form>

【讨论】:

  • Robbie,我正在从 PHP 调用中获取选项到 myMSQL,我已经添加了上面的代码
  • 不是选项。 &lt;option&gt; 标签。
  • POST 说它得到了什么数据?
  • array (size=4) 'gender' => string 'M' (length=4) 'swimOpt' => string 'user10' (length=13) 'formSubmit' => string '提交' (length=6) 'action1' => 字符串 'addSwimmer' (length=10)
  • 它正在工作。 'swimOpt' =&gt; string 'user10' (length=13) 表示正在接收信息。
【解决方案2】:

select 选项必须用带有value 属性的html 标签&lt;option&gt; 包裹,如果它被选中将被发布。 根据您给定的代码,您不会为select 生成options。所以尝试这样的事情:

if ($result->num_rows > 0) {
$options= '';
// output data of each row 
while($row = $result->fetch_assoc()) {
    $options .=  "<option value=\"". $row["swimmer_id"] ."\">" . $row["first_name"]. " " . $row["last_name"]. " - "  . $row["school_name"]. "</option>";
}

} else {
echo "0 results";
}
echo $options;
$conn->close();

现在如果你$_POST 它你会得到swimmer id

【讨论】:

  • 仍然得到不正确的用户。看起来我每次都得到最后一个用户
  • 为值和转义添加双引号,这将与我的答案相同
  • $options .= "&lt;option value=\"". $row["swimmer_id"] ."\"&gt;" . $row["first_name"]. " " . $row["last_name"]. " - " . $row["school_name"]. "&lt;/option&gt;";
  • 能否请您给我们看一下&lt;?php echo $options; ?&gt; 的屏幕截图,DOM 树的屏幕截图,您的选项在&lt;select&gt; 框中回显
  • 如果你将整个&lt;select&gt;框包装到$option变量中会更好
猜你喜欢
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
相关资源
最近更新 更多