Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将下拉列表(HTML)中的选定值传递给PHP_Php_Html - Fatal编程技术网

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

如何将下拉列表(HTML)中的选定值传递给PHP,php,html,Php,Html,我正在动态构建一个选择选项。我从选择选项中选择一个值。(列表的值来自php 如何将所选值传递给PHP <select id ="s1" name="swimopt" class="so"> <?php echo $options; ?> </select>' 这是我从mySQL获取$options的PHP if ($result->num_rows > 0) { $options= ''; // output data

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

<select id ="s1" name="swimopt" class="so">
    <?php echo $options; ?>
</select>' 
这是我从mySQL获取$options的PHP

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();
if($result->num\u rows>0){
$options='';
//每行的输出数据
而($row=$result->fetch_assoc()){
$options.=.$row[“游泳者id”]。$row[“名字”]。$row[“姓氏”]。$row[“学校名称”];
}
}否则{
回显“0结果”;
}
echo$期权;
$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();


?>
$sql=“从性别为'm'的游泳池中选择名字和姓氏”;
$results=mysqli_查询($link,$sql);
如果($results->num_rows>0){
$options='';
//每行的输出数据
而($row=$results->fetch_assoc()){
$fname=$row[“first_name”];
$lname=$row[“姓氏”];
$options.=”.$row[“姓氏”]。$row[“姓氏”]。”;
}
}否则{
回显“0结果”;
}
echo$期权;
$link->close();
?>

我想我明白你的问题所在。你没有设置选择选项。 试试这个:

<?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:
得分2:
地点:
时间:
车道:
姓名:

选择选项必须用html标记
包装,并带有
属性,如果选择了该属性,将发布该属性。 根据给定的代码,您不会为
选择生成
选项
s。请尝试以下操作:

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();
if($result->num\u rows>0){
$options='';
//每行的输出数据
而($row=$result->fetch_assoc()){
$options.=''.$行[“姓氏”]。$行[“姓氏”]。-“$行[“学校名称”]。”;
}
}否则{
回显“0结果”;
}
echo$期权;
$conn->close();

现在,如果您
$\u POST
it,您将获得
游泳者id

显示您的完整代码;)以及表单和php部分的内容。从我的html文件中添加了表单。我没有看到名为“swimopt”的输入。您是否尝试过
print\r($\u POST)
并查看了哪些内容被传递了select在您的表单中位于何处?正如Robbie所说,在提交表单时在PHP端回显变量。使用
print\r($\u POST)
var\u dump($\u POST)
。Robbie,我从对myMSQL的PHP调用中获得选项,我在上面添加了代码而不是选项。
标签。POST显示它得到了什么数据?数组(大小=4)'gender'=>string'M'(长度=4)'swimOpt'=>string'user10'(长度=13)'formSubmit'=>string'Submit'(长度=6)'action1'=>string'addswimber'(长度=10)它正在工作<代码>'swimOpt'=>字符串'user10'(长度=13)
表示它正在接收信息。仍然获取不正确的用户。看起来每次我都会得到最后一个用户为value和escape添加双引号,这将与我的答案
$options.=''相同$行[“名字”]。" " . $行[“姓氏”]。" - "  . $第[“学校名称”]行。"";
您能否向我们展示
的屏幕截图,即DOM树的屏幕截图,您的选项会在
框中显示,如果您将整个
框包装到
$option
变量中会更好
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();