php if语句html选项值

时间:2013-03-12 09:26:28

标签: php option if-statement

假设您有以下html select语句

<select>
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>

现在我想运行一个php if elseif语句,

if (option value = newest) {
// Run this
}
elseif ( option value = best sellers ) {
// Run this
}

等。但是我不知道在if elseif语句中放什么。换句话说,而不是'option value = newest'(我知道这是不正确的),我可以放在那里,如果选择最新,它将执行if语句,或者如果选择了畅销书,它将执行elseif语句?

5 个答案:

答案 0 :(得分:11)

为您的选择命名。

<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>

在你的PHP中,你会这样做:

$_POST['selectedValue'];

如果我是你,我宁愿选择 switch-case ,还有两个以上的条件。

示例:

switch($_POST['selectedValue']){
case 'Newest':
    // do Something for Newest
break;
case 'Best Sellers':
    // do Something for Best seller
break;
case 'Alphabetical':
    // do Something for Alphabetical
break;
default:
    // Something went wrong or form has been tampered.
}

答案 1 :(得分:6)

首先在您的选择上添加一个名称:

<select name="demo">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>

然后

if ($_POST['demo'] === 'Newest') {
// Run this
}
elseif ( $_POST['demo'] === 'Best Sellers' ) {
// Run this
}

switch($_POST['demo']){
    case 'Newest' : 
        //some code;
        break;
    case 'Best Sellers':
        //some code;
        break;
    default:
        //some code if the post doesn't match anything
}

答案 2 :(得分:0)

<select>应具有name属性,例如<select name="sortorder">。然后你可以说

if ($_REQUEST['sortorder'] == 'Newest') {
  // TODO sortorder 'Newest' was selected.
}

如果你知道表单数据是通过HTTP GET还是HTTP POST进行的,你可以分别使用$_GET['sortorder']$_POST['sortorder']

答案 3 :(得分:0)

我认为阅读友好版本将是:

switch($option){
    case 'Newest':
        runNewestFunction();
    break;
    case 'Best Sellers':
        runBestSellersFunction();
    break;
    case 'Alphabetical':
        runAlphabeticalFunction();
    break;
    defaut:
        runValidationRequest();
}

另外,请添加名称属性<select name="option">

答案 4 :(得分:0)

<?php
if ($_POST['com'] == 1 && $_POST['dept_1'] == 'permanent' ) 
{
  $sel_id= "select MAX(emp_id) from employee  ";
  $sel_exeid=mysql_query($sel_id)  or die(mysql_error());;
  while($empid= mysql_fetch_array($sel_exeid))
  {
      $result = $empid[0];
      echo $result;
  } 
}
?>

<td>
<input class="easyui-validatebox" required="true" name="eid" type="text"id="eid" value="<?php echo ($result);?>" placeholder="Enter Employee Id"/>
</td>