如何使用GET从php中的select选项中检索数据

时间:2013-09-06 12:18:14

标签: php get echo option

您好我的代码有问题。我需要从select选项中检索数据,该选项是从sql中选择的。这是代码:

<form action="zadany.php" method="get">
    <label for="select">
Komu zadat novej úkol:
        <select name="users">
<?php
$sql = "SELECT * FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
    $id=$row["id"];
    $username=$row["username"];
    $options.="<OPTION VALUE=\"$id\">".$username;
}
?>
    <option>
        <? echo $options ?>
    </option>
</select>

<br />
Typ úkolu:
<table border="1">

<input type="radio" name="ukol" value="bla1">Ukol 1</input>
<input type="radio" name="ukol" value="bla2">Ukol 2</input>
<input type="radio" name="ukol" value="bla3">Ukol 3</input>

</table>
<br />
<input type="submit" value="zadej úkol">

</form>

行动是:

<?php
include 'core/init.php';
protect_page();
include 'includes/overall/header.php';


echo $_GET['users'];

switch ($_GET["ukol"]) {
    case "bla1":
        echo 'ukol 1';
        break;
    case "bla2";
        echo 'ukol 2';
        break;
    case "bla3":
        echo 'ukol 3';
        break;
    default: 
        echo 'nezadany ziadny ukol';
    }
include 'includes/overall/footer.php';
?>

所以我需要例如回显选择菜单中的选定选项。有人可以帮我解决这个问题吗?非常感谢每个帖子和答案。

1 个答案:

答案 0 :(得分:0)

您的PHP代码生成以下代码:

<option>
   <OPTION value="your id #1"> Username
</option>

你可以尝试:

<select name="users">
<?php
$sql = "SELECT * FROM users";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
    $id=$row["id"];
    $username=$row["username"];
    $options.="<OPTION VALUE=\"$id\">".$username."</OPTION>";
}
?>

    <? echo $options ?>

</select>