检查从<select>标签中选择的选项值

时间:2015-10-02 10:40:49

标签: php

&lt; form action =&#34;&#34;方法=&#34; POST&#34;&GT;      &lt; select name =&#39;选择&#39;&gt;         &LT;选项&GT;芒果&LT; /选项&GT;         &LT;选项&GT;香蕉&LT; /选项&GT;      &LT; /选择&GT; &LT; /形式&GT; 如果我有这个选项值,我怎么知道设置了哪个值而不是传递值。 if(已设置($ _ POST [&#39;选择&#39;])){       echo&#34; Something&#34 ;; } 上面是选择标签,它工作正常,但我尝试给芒果和香蕉的名称,但没有得到我选择的价值。有人请帮忙。谢谢!

3 个答案:

答案 0 :(得分:1)

选项需要一些值,例如:

<form action="" method="POST">
<select name='choose'>
<option value="mango" >mango</option>
<option value="banana">banana</option>
</select>
</form>

PHP: 将更改设置为()到isset()并添加if-else条件,以处理不同的输入

if( isset( $_POST['choose'] ) ){
    if( $_POST['choose'] === "mango" ) {
        echo "do the mango";
    } elseif( $_POST['choose'] === "banana" ) {
        echo "do the banana";
    } else {
        echo "nothing chosen";
    }
} 

将其传递给varibale

$chosen_fruit = "none";

// You can also pass the value to a variable
if( isset( $_POST['choose'] ) ){
    if( $_POST['choose'] === "mango" ) {
        $chosen_fruit = "mango";
    } elseif( $_POST['choose'] === "banana" ) {
       $chosen_fruit = "banana";
    }
} 


// here you can use the value of the variable
echo $chosen_fruit;// echo outside the if condition

如果您想了解有关变量范围的更多信息,请查看here

答案 1 :(得分:0)

您必须为每个选项设置值

<form action="" method="POST">
<select name='choose'>
<option value="mango" >mango</option>
<option value="banana">banana</option>
</select>
</form>

if(isset($_POST['choose'])){ //replace is set to isset
echo "Something";
} 

答案 2 :(得分:0)

更改为

 <option value="mango">mango</option>
 <option value="banana">banana</option>

isset中有空格,所以改变它:

 $data="";
 if(isset($_POST['choose'])){
   $value = $_POST['choose'];
   if($value=="fruitValue") {
    $data = "do_soemthing"; 
   }    
 } 

$data访问数据变量。