在PHP中传递所选项目下拉列表的值

时间:2015-02-12 07:59:13

标签: php html selectlist

我有一个html表单并使用php从该表单中间的数据库获取用户名,我想在用户点击按钮时提交选择选项值。

File1:

<link href="users_forms.css" rel="stylesheet" type="text/css" />

<div id="rightcolumn">
    <form method="post" action="user_remqry.php">
        <br />
        <legend> Remove Users </legend>
        <fieldset>
            <br />


            <?php
            include'connect.php';
            $q=mysqli_query($con,  "SELECT username FROM users_allow");

            echo "<select name=uname_selected''  ><option value=0>Select a username</option>";
            WHILE($row=mysqli_fetch_array($q))
            {
                echo "<option name='uname_remove' value=$row[username]>".$row[username]."</option>";
            }
            echo "</select>";
            ?>


            <br /> <br /> <br /> <input type="submit" value="Delete User" />
        </fieldset>
    </form>
</div>

文件2(user_remqry.php):

<?php
$uname = $_POST["uname_selected"];
echo 'I am going to remove =  '.$uname;
?>

我只想传递选择列表中所选项目的值。

1 个答案:

答案 0 :(得分:0)

您的代码中需要更改的内容

include 'connect.php'; //please add space
$q=mysqli_query($con,  "SELECT username FROM users_allow");

echo "<select name='uname_selected'>"; //note the position of single quote
echo "<option value='0'>Select a username</option>";
while($row=mysqli_fetch_array($q)){
    echo "<option value='".$row['username']."'>".$row['username']."</option>"; 
} //don't have to put name attribute at the option. Put quotes around $row['username']. Note the single quote surrounding the username
echo "</select>";
正如杰罗德夫所说,没有必要为这个选项命名。在下一页上,只需要调用select的名称。在您的情况下$_POST['uname_selected']

相关问题