php if 语句 html 选项值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15357450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php if statement html option value
提问by Marlboro Goodluck
Suppose you have the following html select statement
假设您有以下 html select 语句
<select>
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
Now I want to run a php if elseif statement that says,
现在我想运行一个 php if elseif 语句,它说,
if (option value = newest) {
// Run this
}
elseif ( option value = best sellers ) {
// Run this
}
etc. But I don't know what to put inside the if elseif statement. In other words instead of 'option value = newest' (which I know is incorrect), what can I put there so that if newest is selected it will execute the if statement, or if best sellers is selected it will execute the elseif statement?
等等,但我不知道在 if elseif 语句中放什么。换句话说,不是'option value = newest'(我知道这是不正确的),我可以在那里放什么,以便如果选择最新的它会执行if语句,或者如果选择了畅销书它会执行elseif语句?
回答by Karma
Give name to your select.
为您的选择命名。
<select name="selectedValue">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
in your PHP, you will do:
在您的 PHP 中,您将执行以下操作:
$_POST['selectedValue'];
$_POST['selectedValue'];
if I were you, I would prefer a switch-caseincase, there are more than 2 conditions.
如果我是你,我更喜欢switch-caseincase,有 2 个以上的条件。
Example:
例子:
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.
}
回答by darkheir
First put a name on your select:
首先在您的选择上命名:
<select name="demo">
<option value="Newest">Newest</option>
<option value="Best Sellers">Best Sellers</option>
<option value="Alphabetical">Alphabetical</option>
</select>
Then
然后
if ($_POST['demo'] === 'Newest') {
// Run this
}
elseif ( $_POST['demo'] === 'Best Sellers' ) {
// Run this
}
or
或者
switch($_POST['demo']){
case 'Newest' :
//some code;
break;
case 'Best Sellers':
//some code;
break;
default:
//some code if the post doesn't match anything
}
回答by Oswald
The <select>should have a nameattribute, e.g <select name="sortorder">. You could then say
本<select>应具有name的属性,例如<select name="sortorder">。然后你可以说
if ($_REQUEST['sortorder'] == 'Newest') {
// TODO sortorder 'Newest' was selected.
}
If you know whether the form data is comming in via HTTP GET or HTTP POST, you could use $_GET['sortorder']or $_POST['sortorder']respectively.
如果您知道表单数据是通过 HTTP GET 还是 HTTP POST 传入,则可以分别使用$_GET['sortorder']或$_POST['sortorder']。
回答by Unamata Sanatarai
I think the read-friendly version would be:
我认为阅读友好的版本是:
switch($option){
case 'Newest':
runNewestFunction();
break;
case 'Best Sellers':
runBestSellersFunction();
break;
case 'Alphabetical':
runAlphabeticalFunction();
break;
defaut:
runValidationRequest();
}
also, please do add the name attribute <select name="option">
另外,请添加 name 属性 <select name="option">

