如何检查是否使用 PHP 检查了两个单选按钮之一?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18432104/
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
How do I check if one of two radio buttons is checked with PHP?
提问by Treps
How do I check if one of two radio buttons is checked with PHP?
如何检查是否使用 PHP 检查了两个单选按钮之一?
HTML
HTML
<form action="" method="post">
<b>Yes or No?</b><br>
Yes <input type="radio" name="template" value="Yes">
NO <input type="radio" name="template" value="No"><br><br>
<input type="submit" name="send" value="Skicka internlogg">
</form>
I don't want one of this two radio buttons to be checked from the beginning. I want an if-statement that continue with the code if one of them are checked by the user. And if not I want to print an error code like "Please fill the whole form" or something. How?
我不希望从一开始就检查这两个单选按钮中的一个。如果用户检查其中一个,我想要一个继续代码的 if 语句。如果不是,我想打印一个错误代码,比如“请填写整个表格”之类的。如何?
I then want to check for multiple statements from radio buttons and text boxes, how?
然后我想从单选按钮和文本框中检查多个语句,如何?
if($_POST['sr'] && $_POST['relevant'] && if(isset($_POST['article'])) && if(isset($_POST['template'])) && $_POST['text']) {}
?????
if($_POST['sr'] && $_POST['relevant'] && if(isset($_POST['article'])) && if(isset($_POST['template'])) && $_POST['text']) {}
?????
回答by Vahid Hallaji
if(isset($_POST['template'])){
$template = $_POST['template'];
//do more stuff
}
else{
echo "Please fill the whole form."; //error
}
回答by Michael Irigoyen
Upon form submission, check to see if the POST variable "template" is not set using isset
.
提交表单后,检查 POST 变量“模板”是否未使用isset
.
The !
in front of isset
means "not". Therefore, it is checking to see if "template" is notset.
在!
前面isset
的意思是“不”。因此,它正在检查是否未设置“模板” 。
You should check each element of your form separately so you can provide constructive feedback to the end-user on what they need to do to resolve the error message. This is considered best practiceand something you should do on any form, regardless how big or small.
您应该分别检查表单的每个元素,以便向最终用户提供有关他们需要做什么来解决错误消息的建设性反馈。这被认为是最佳实践,您应该以任何形式执行此操作,无论大小。
<?php
// Form submitted
if(isset($_POST['send'])) {
// CHECK FOR ERRORS
// Check for radio button value
if(!isset($_POST['template'])) {
// No value for template, show error message
$e['template'] = "<p><strong>Please select Yes or No.</strong></p>";
}
// Check the value of text box
if(!isset($_POST['relevant'])) {
$e['relevant'] = "<p><strong>Please fill out the Relevant field.</strong></p>";
}
// If no errors occurred, finish processing the form
if(!is_array($e)) {
// Do stuff (db update, email send out, etc.)
// Show thank you message
echo "<p>Thank you. We have received your submission.</p>";
exit();
}
}
?>
<form action="" method="post">
<?php echo $e['relevant']; ?>
<p><label for="relevant">Relevant:</label><input type="text" name="relevant" id="relevant" value="<?php echo htmlspecialchars($_POST['relevant']); ?>" /></p>
<?php echo $e['template']; ?>
<p>Yes or No?</p>
<p><label for="yes">Yes</label><input type="radio" name="template" id="yes" value="Yes" <?php if($_POST['template'] == "Yes") { echo 'checked="checked"'; } ?> /></p>
<p><label for="no">No</label><input type="radio" name="template" id="no" value="No" <?php if($_POST['template'] == "No") { echo 'checked="checked"'; } ?> /></p>
<p><input type="submit" name="send" value="Skicka internlogg" /></p>
</form>
EDIT:To address you wanting to do the check all at once, which you should not get in the habit of doing, you could do something like this:
编辑:为了解决您想要一次进行所有检查的问题,您不应该养成这样做的习惯,您可以执行以下操作:
if(!isset($_POST['template']) || !isset($_POST['relevant']) || !isset($_POST['sr']) || ... ) {
$error = "<p>Please fill out all form fields.</p>";
}
Then echo the error out in the form as in my example above.
然后以我上面的示例中的形式回显错误。
回答by Funk Forty Niner
EDIT:
编辑:
To check all POST values at once, you can create an array of required fields and loop through that.
要一次检查所有 POST 值,您可以创建一组必填字段并循环遍历。
$required_fields = array("name", "address", "phone", "email");
foreach ($require_fields as $field) {
if (!strlen($_POST[$field])) {
echo "$field cannot be empty";
}
}
Source: https://stackoverflow.com/a/4496301/1415724
来源:https: //stackoverflow.com/a/4496301/1415724
Original
原来的
In using my example below, radio buttons need to be treated as an array using []
when using foreach
, as in name="template[]"
for all radio buttons used. Otherwise, there will be an error such as Warning: Invalid argument supplied for foreach()
.
在使用我下面的示例时,需要将单选按钮视为使用[]
when using的数组foreach
,就像name="template[]"
所有使用的单选按钮一样。否则会出现诸如Warning: Invalid argument supplied for foreach()
.
This enables you to use it in another way, such as checkboxes should you decide to use that instead of radio buttons in another application.
这使您能够以另一种方式使用它,例如如果您决定使用复选框而不是其他应用程序中的单选按钮。
Here is an example:
下面是一个例子:
<?php
$name = $_POST['template'];
if(isset($_POST['template'])) {
echo "You chose: <br>";
echo "<ul>";
foreach ($name as $template){
echo "<li>" .$template."</li>";
}
echo "</ul>";
} // isset
else {
echo "You did not make a choice.";
}
?>
<form action="" method="post">
<b>Yes or No?</b><br>
Yes <input type="radio" name="template[]" value="Yes">
NO <input type="radio" name="template[]" value="No"><br><br>
<input type="submit" name="send" value="Submit">
</form>
回答by Moein Hosseini
You can use isset
function in your php code.
您可以isset
在 php 代码中使用函数。
if (isset($_POST['template'])){
// Yes or No selected.
}
回答by Lkopo
if(isSet($_POST['template']) && $_POST['template'])
{
// dsth
}
else
echo 'Please fill the whole form';
You need to edit your PHP code ^
您需要编辑您的 PHP 代码 ^