2011-05-06 122 views
2
<select name="gamelist" id="gamelist"> 
<option value="1">Backgammon</option> 
<option value="2">Chess</option> 
</select> 
<input type="submit" name="submit" id="submit" value="Submit" /> 

我想抓取选定的值并将其放入var 任何想法? 谢谢php如何从下拉列表中获取选定的值?

+1

嗯,这是基本的形式处理,你应该谷歌它 – morgar 2011-05-06 23:49:17

回答

2

取决于您的表单标记。

<form method="post"> 

将价值传递到$ _POST [ '游戏列表']

虽然

<form method="get"> 

将通过价值$ _GET [ '游戏列表']

Ofcourse,只点击提交按钮后。正如摩尔格所说,这是非常基本的形式游行。我怀疑你是否使用过Google或遵循教程,这几乎是人们在使用表单和PHP时学到的第一件事情。在这里,我们的arent自己给你完全的解决方案,以此为例子,创建整页:

if($_SERVER['REQUEST_METHOD'] == "Y") { 
    $choice = $_Y['gamelist']; 
    // other stuff you want to do with the gamelist value 
} else { 
    echo '<form method="Y" action="file.php">'; 
    // the rest of your form 
    echo '</form>'; 
} 

与换成y GET或POST。

0
$choice = $_POST['gamelist'] 

如果它是一个POST,或$ _GET如果不是。

+0

我得到'未定义指数:gamelist' – Patrioticcow 2011-05-06 23:52:20

+0

你用的print_r($ _ REQUEST)得到了什么? – matt 2011-05-06 23:56:06

1
$choice = $_REQUEST['gamelist']; //works with get or post 
相关问题