PHP 中下拉选择项的传值
Pass value of selected item of dropdown in PHP
我有一个 html 表单,并使用 php 从该表单中间的数据库中获取用户名,我想在用户单击按钮时提交 select 选项值。
文件 1 :
<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;
?>
我只想传递 select 列表的 selected 项的值。
您的代码中需要更改的内容很少
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>";
如 Jerodev 所述,无需为选项输入名称。在接下来的页面中,只需调用 select 的名称即可。你的情况 $_POST['uname_selected']
我有一个 html 表单,并使用 php 从该表单中间的数据库中获取用户名,我想在用户单击按钮时提交 select 选项值。
文件 1 :
<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;
?>
我只想传递 select 列表的 selected 项的值。
您的代码中需要更改的内容很少
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>";
如 Jerodev 所述,无需为选项输入名称。在接下来的页面中,只需调用 select 的名称即可。你的情况 $_POST['uname_selected']