php if 语句 html 选项值

新手上路,请多包涵

假设您有以下 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 = latest’(我知道这是不正确的),我可以放什么以便如果选择了最新的它将执行if语句,或者如果选择了畅销书,它将执行elseif语句?

原文由 Marlboro Goodluck 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 363
2 个回答

为您的选择命名。

 <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 incase,有两个以上的条件。

例子:

 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.
}

原文由 Keval Domadia 发布,翻译遵循 CC BY-SA 3.0 许可协议

首先在您的选择上输入名称:

 <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
}

原文由 darkheir 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题