简体   繁体   English

使用 PHP 的单选按钮中的必填字段验证

[英]Required field validation in radio button using PHP

I am trying to add required field validation to a page where there are 5 radio buttons with the same name.我正在尝试将必填字段验证添加到有 5 个同名单选按钮的页面。 If the user clicks the button to proceed to the next page an error should be displayed, below is the code I am currently using but it is not doing anything如果用户单击按钮进入下一页,则应显示错误,以下是我当前正在使用的代码,但它没有做任何事情

<?php
if (empty($_POST) === false) {
$required_fields = array('q1');
foreach($_POST as $key=>$value) {
if (empty($value) && in_array($key, $required_fields) === true) {
    $errors[] = 'Please select a number between 1-5';
    break 1;
    }
}
if (empty($errors) === true) {
    if(!isset($_POST['q1'])){ 
    $errors[] = "Please select a number between 1-5"; 
} 

}
}
?>      

<h1>Assessment</h1>

<?php
if (isset($_GET['success']) && empty($_GET['success'])) {

} else {
if (empty($_POST) === false && empty($errors) === true) {
$updateassessment = array(
    'q1'                => $_POST['q1'],
);

updateassessment($updateassessment);
header ('Location: question2.php');
exit();

} else if(empty($errors) === false){
echo output_errors($errors);
}
?>

<form action="" method="post">

<p class="p1">
Question 1</p>
<p class="p4">
<?php
$result = mysql_query("SELECT * FROM traits WHERE Question = 'q1'");

while($row = mysql_fetch_array($result))
  {
  echo $row['fromtrait'] . ' - ' . $row['totrait'];
  echo "<br />";
  }


?></p>

<p class="p3">
<input type="radio" name="q1" value="1" />
1
<input type="radio" name="q1" value="2" />
2
<input type="radio" name="q1" value="3" />
3
<input type="radio" name="q1" value="4" />
4
<input type="radio" name="q1" value="5" />
5


</p><br><br>
  <input type="image" src="Images/image1.png" name="submit" align="right"/>
</form>

I got this to work by adding我通过添加使这个工作

if (empty($errors) === true) {
    if(!isset($_POST['q1'])){ 
    $errors[] = "Please select a number between 1-5"; 
} 

}

You have to set action="" to action=" <filename> " (in your form) else PHP won't know where to "go" after user submits the form.您必须将 action="" 设置为 action=" <filename> " (在您的表单中),否则 PHP 在用户提交表单后将不知道“去”哪里。 Filename refers to the php-file.文件名是指 php 文件。

by it's nature PHP is a inefficient way to validate forms because the page has to reload for any PHP checks to be done.从本质上讲,PHP 是验证 forms 的低效方法,因为必须重新加载页面才能完成任何 PHP 检查。 perhaps a client side solution can be implemented, but if not try this to see if it does what you're looking for...也许可以实现客户端解决方案,但如果不尝试这个,看看它是否符合您的要求......

$required_fields = array('q1');
foreach ($required_fields as $field) {
    if (isset($_POST[$field]) && !empty($_POST[$field])) {
         echo $field." was set<br>\n";
    } else {
        echo "validation for $field failed!<br>\n";
    }
}

Also... is Javascript an option?另外...... Javascript 是一个选项吗? jquery ValidationEngine is quite useful and has checks for many of your field requirements, and validate before the form is submitted to mitigate needs tor PHP to do the checks. jquery ValidationEngine 非常有用,可以检查您的许多字段要求,并在提交表单之前进行验证,以减轻 PHP 进行检查的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM