如何将下拉列表(HTML)中的选定值传递给PHP

时间:2015-11-02 21:19:53

标签: php html

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

for

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

当我选择一个值时,请尝试

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

未显示所选值

请帮忙

echo $_POST['swimopt'];

这是我的PHP从mySQL获取$选项

 <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>

截图:

Image of Select options in UI

List inside dropdown

选项回显到选择框:

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();

PHP文件代码

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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

select选项必须包含带有<option>属性的html标记value,如果已选中,则会发布该属性。 根据您给出的代码,您不会为option生成select个。所以尝试这样的事情:

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