简体   繁体   English

表单验证,数百个复选框

[英]form validation, hundreds of checkboxes

So I'm building a checkbox form with well over 200 questions (don't ask lol), and I don't want the user to be able to go to the results page without at LEAST having one checkbox checked. 因此,我正在构建一个包含200多个问题的复选框表单(不要问笑),并且我不希望用户至少在未选中一个复选框的情况下转到结果页面。 Problem is, writing something like this: 问题是,编写如下代码:

if (isset($_POST['q1']) || isset($_POST['q2']) || isset($_POST['q3'])..............)

for hundreds of questions is going to be... long. 数以百计的问题将会很长。 Is there any way to make it shorter? 有什么办法可以缩短它?

the code snippet: 代码段:

<input type="checkbox" name="q1" onclick="KeepCount()" value="1">Question 1<br>
<input type="checkbox" name="q2" onclick="KeepCount()" value="1">Question 2<br>
<input type="checkbox" name="q3" onclick="KeepCount()" value="1">Question 3<br>
<input type="checkbox" name="q4" onclick="KeepCount()" value="1">Question 4<br>
<input type="checkbox" name="q5" onclick="KeepCount()" value="1">Question 5<br>
<input type="checkbox" name="q6" onclick="KeepCount()" value="1">Question 6<br>
    <input type="submit" name="button" class="button" value="Submit" /> 
</form>

 <?php 
include 'defaults.php';
if(isset($_POST['button'])) {
   if(isset($_POST['q1'])) {$_SESSION['q1v'] = 1;};
   if(isset($_POST['q2'])) {$_SESSION['q2v'] = 1;};
   if(isset($_POST['q3'])) {$_SESSION['q3v'] = 1;};
   if(isset($_POST['q4'])) {$_SESSION['q4v'] = 1;};
   if(isset($_POST['q5'])) {$_SESSION['q5v'] = 1;};
   if(isset($_POST['q6'])) {$_SESSION['q6v'] = 1;};

   header('Location: results.php');

}

Make them an array: 使它们成为数组:

<input type="checkbox" name="q[1]" onclick="KeepCount()" value="1">Question 1<br>
<input type="checkbox" name="q[2]" onclick="KeepCount()" value="1">Question 2<br>
etc...

Then to check that at least one is checked (if none are checked then the q array will not exist): 然后检查是否至少检查了一个(如果未选中,则q数组将不存在):

if(isset($_POST['q']))

And simply this for the session vars: 对于会话变量,简单地说就是这样:

$_SESSION['q'] = $_POST['q'];

So maybe: 所以也许:

if(isset($_POST['q'])) {
    $_SESSION['q'] = $_POST['q'];
}

如果使所有复选框的名称都像:name =“ myCheckBoxes []”,则可以像数组一样处理发布值。

<input type="checkbox" name="q[]" onclick="KeepCount()" value="1">Question 1<br>
<input type="checkbox" name="q[]" onclick="KeepCount()" value="1">Question 2<br>
<input type="checkbox" name="q[]" onclick="KeepCount()" value="1">Question 3<br>
<input type="checkbox" name="q[]" onclick="KeepCount()" value="1">Question 4<br>
<input type="checkbox" name="q[]" onclick="KeepCount()" value="1">Question 5<br>
<input type="checkbox" name="q[]" onclick="KeepCount()" value="1">Question 6<br>

 <?php 
include 'defaults.php';
if(isset($_POST['q'])) {
  $questions=$_POST['q'];   <-- $questions is an array and easier to work with

   header('Location: results.php');

}

Since you have a naming convention you could just do this in a loop: 由于您有命名约定,因此可以循环执行此操作:

$checkbox_count = 200;
$prefix = 'q';

for($i = 1; $i <= $checkbox_count; $i++){
   $key = sprintf("%s%u", $prefix, $i);
   if(isset($_POST[$key])){
      $_SESSION[$key.'v'] = 1;
   }
}

First you can make an array like bellow 首先,您可以制作一个像下面这样的数组

<input type="checkbox" name="myarray[]" onclick="KeepCount()" value="1">Question 1<br> <input type="checkbox" name="myarray[]" onclick="KeepCount()" value="1">Question 2<br>

then take the value as an array like bellow 然后将值作为一个像波纹管一样的数组

$myarray = $_POST['myarray'];` $ myarray = $ _POST ['myarray'];`

then check them with php foreach, are they empty 然后用php foreach检查它们,它们是否为空

 foreach($myarray as $key=>$value){
  if(!isset($value)){
  $error_status = true}

if status is true, you can redirect him/her in any page like bellow 如果状态为true,则可以在以下页面中重定向他(她)

 if($error_status==true){
  header('location: anypage.php');
  }
  else{
  header('location:results.php');
  }

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

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