[英]How do I check if one of two radio buttons is checked with PHP?
如何檢查是否使用PHP檢查了兩個單選按鈕中的一個?
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>
我不希望從頭開始檢查這兩個單選按鈕中的一個。 如果用戶檢查其中一個,我想要一個if-statement繼續代碼。 如果不是,我想打印一個錯誤代碼,如“請填寫整個表格”或其他東西。 怎么樣?
然后我想檢查單選按鈕和文本框中的多個語句,怎么樣?
if($_POST['sr'] && $_POST['relevant'] && if(isset($_POST['article'])) && if(isset($_POST['template'])) && $_POST['text']) {}
?????
if(isset($_POST['template'])){
$template = $_POST['template'];
//do more stuff
}
else{
echo "Please fill the whole form."; //error
}
提交表單后,檢查是否未使用isset
設置POST變量“template”。
的!
在isset
前面意味着“不”。 因此,它正在檢查是否未設置“模板”。
您應該單獨檢查表單的每個元素,以便為最終用戶提供有關解決錯誤消息所需操作的建設性反饋。 這被認為是最佳實踐 ,您應該在任何形式上做,無論大小。
<?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>
編輯:為了解決你想要一次性檢查, 你不應該養成做的習慣 ,你可以做這樣的事情:
if(!isset($_POST['template']) || !isset($_POST['relevant']) || !isset($_POST['sr']) || ... ) {
$error = "<p>Please fill out all form fields.</p>";
}
然后在表單中回顯錯誤,如上例所示。
您可以在PHP代碼中使用isset
函數。
if (isset($_POST['template'])){
// Yes or No selected.
}
要一次檢查所有POST值,您可以創建一個必需字段數組並循環顯示。
$required_fields = array("name", "address", "phone", "email");
foreach ($require_fields as $field) {
if (!strlen($_POST[$field])) {
echo "$field cannot be empty";
}
}
資料來源: https : //stackoverflow.com/a/4496301/1415724
在使用下面的示例時,使用foreach
,使用[]
需要將單選按鈕視為數組,對於所有使用的單選按鈕, name="template[]"
。 否則,將出現錯誤,例如Warning: Invalid argument supplied for foreach()
。
這使您可以以其他方式使用它,例如復選框,如果您決定使用它而不是另一個應用程序中的單選按鈕。
<?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>
if(isSet($_POST['template']) && $_POST['template'])
{
// dsth
}
else
echo 'Please fill the whole form';
您需要編輯PHP代碼^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.