从php变量设置选项值

时间:2017-02-26 18:16:21

标签: javascript php html

<form method="post" id="employee" action="change.php">
<select id="employeename" name="employeename" onchange="this.form.submit()">
 <?php 
while ($row = mysql_fetch_array($result))
{
    echo "<option value='".$row['name']."'>".$row['name']."</option>";
}
?>    
</select>
</form>

当选择值更改时,将提交表单

提交表单后,它将重定向到同一页面。如何使用我刚刚选择的选项值设置所选的选项值?

2 个答案:

答案 0 :(得分:2)

以下列方式更改您的表单,尤其是<select>下拉列表

<form method="post" id="employee" action="change.php">
    <select id="employeename" name="employeename" onchange="this.form.submit()">
        <?php 
            while ($row = mysql_fetch_array($result)){
                $output = "<option value='".$row['name']."'";
                if($_POST['employeename'] == $row['name']){
                    $output .= " selected='selected'";
                }
                $output .= ">".$row['name']."</option>";
                echo $output;
            }
        ?>    
    </select>
</form>

答案 1 :(得分:0)

    <form method="post" id="employee" action="change.php">
    <select id="employeename" name="employeename" onchange="this.form.submit()">
     <?php 
    while ($row = mysql_fetch_array($result))
    {
   if($_POST['employeename']==$row['name'])
        echo "<option value='".$row['name']."' selected>".$row['name']."</option>";
else 
   echo "<option value='".$row['name']."'>".$row['name']."</option>";
    }
    ?>    
    </select>
    </form>
如果您在可用列表中找到所选键,则

仅标记为已选中。