我想知道当我点击提交时用户在两个选择框中选择了什么。 但我无法回复POST ['submit']中的选定值。这是我试过的代码。
有人可以建议我:提交时如何回显所选的值。
<?php
require 'init.php';
if (isset($_POST['submit']))
{
foreach($_POST['role'] AS $ID => $Value)
{
$role = $Value; //echo $letter.'<br>';
$user = $_POST['user'][$ID]; //echo $serial.'<br>';
echo $user.'<br>';
echo $role;
}
}
else
echo 'error';
?>
<!DOCTYPE html>
<html lang="<?php echo _SITE_LANGUAGE ?>">
<body>
<?php
echo '<form action="" method="post">';
echo 'Select a role:
<select name="role">';
$query = $db->prepare('SELECT * FROM boolean_role WHERE role_id>2');
$query->execute();
echo '<option selected="selected">Choose a role </option>';
while ($row = $query->fetch(PDO::FETCH_ASSOC))
{
echo '<option value='.$row['role_id'].'>'.$row['role_name'].'</option>';
}
echo '</select><br>';
echo 'Select an user:
<select name="user">';
$query1 = $db->prepare('SELECT * FROM boolean_users');
$query1->execute();
echo '<option selected="selected">Choose a user </option>';
while ($row = $query1->fetch(PDO::FETCH_ASSOC))
{
echo '<option value='.$row['id'].'>'.$row['username'].'</option>';
}
echo '</select><br>';
echo '<input type="submit" value="submit">';
echo '</form>';
?>
</body>
</html>
答案 0 :(得分:0)
您不需要foreach循环。
<?php
require 'init.php';
if (isset($_POST['submit']))
{
$user = $_POST['user'];
$role = $_POST['role'];
echo $user;
echo $role;
}
else
echo 'error';
?>