如何在php中获取select的索引

时间:2016-04-18 15:46:17

标签: php

<span>Level</span><br>
 <select name="exp" id="exp" required>
   <option disabled selected hidden>Choose your level</option>
   <option>without exp</option>
   <option>up to 1 month</option>
   <option>up to 3 months</option>
   <option>up to 6 months</option>
   <option>1 year or more</option>
 </select>

PHP:

$exp = "";      

if(!empty($_POST)){
   $exp = $_POST['exp'];     //here I get only the value??
   calcularIMC($exp);    
}

function calcularIMC($exp){            // comparisons I want to do
    if($exp == "INDEX 0"){ ...... }   // how I can get the value to do this comparations?
    if($exp >= "INDEX 1"){ ...... }
}

对于测试我做了变量$exp以字符串的形式存储。我想知道如何在上面的例子中得到索引进行比较。

如何获取select的索引?

1 个答案:

答案 0 :(得分:3)

您需要为选项设置值,然后这些值将是提交给您服务器的值。

 <select name="exp" id="exp" required>
   <option disabled selected hidden>Choose your level</option>
   <option value="0">without exp</option>
...
 </select>