我有一个简单的下拉选择菜单。
<div id="select">
<select class="select">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
如何获取用户选择的值并将其存储到php变量中?
答案 0 :(得分:3)
$selected = $_POST['somename'];
HTML
<form action="somephpfile.php" method="post">
<div id="select">
<select class="select">
<select name="somename"> <!-- you missed this -->
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</form>
答案 1 :(得分:2)
你错过了select和form标签中的“name”属性。试试这个:
<HTML><BODY>
<?PHP
$sel_year= $_POST['select'];
echo $sel_year
?>
<FORM method="post" action="...your-php-file-name-here...">
<div id="select">
<select name="select">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</FORM>
</BODY></HTML>
答案 2 :(得分:1)
<form action="" method="post">
<div id="select">
<select class="select" name="selectoptionname">
<option value="year 1">year 1</option>
<option value="year 2">year 2</option>
<option value="year 3">year 3</option>
</select>
</div>
</form>
表单提交时,您可以使用$_POST['selectoptionname']
答案 3 :(得分:0)
将其放入表格中,然后使用$ _POST ['select']