如何将选择值存储到php变量中

时间:2014-02-03 10:08:46

标签: php

我有一个简单的下拉选择菜单。

<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变量中?

4 个答案:

答案 0 :(得分:3)

somephpfile.php中的

$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']