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. 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):
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'];`
then check them with php foreach, are they empty
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
if($error_status==true){
header('location: anypage.php');
}
else{
header('location:results.php');
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.