I am making a form that contains two questions on witch the user can answer by checking some checkboxes.
Question 1
“Do’s”
Answer:
Checkbox 1: sit <input type="checkbox" name=”dos[]” value= “sit”>
Checkbox 2: stand<input type="checkbox" name=”dos[]” value “stand”>
Checkbox 3: run <input type="checkbox" name=”dos[]” value= “run”>
Checkbox 4: walk <input type="checkbox" name=”dos[]” value= “walk”>
Question 2
“Dont’s”
Answer:
Checkbox 1: sit <input type="checkbox" name=”donts[]” value= “sit”>
Checkbox 2: stand <input type="checkbox" name=” donts[]” value “stand”>
Checkbox 3: run <input type="checkbox" name=” donts[]” value= “run”>
Checkbox 4: walk <input type="checkbox" name=” donts[]” value= “walk”>
Now, I would like to do a check if the user did not check the same checkbox on both questions. So an error message appears when in question one and in question two “sit” is checked.
I have dese codes to output
<?
php $count=count($dos);
for ($i=0; $i<$count; $i++) {
echo $dos[$i];
}
?>
<?php
$count=count($donts);
for ($i=0; $i<$count; $i++) {
echo $donts[$i];
}
?>
I think I have to find a way to check in both arrays if there is an element that is the same.
Is there someone here who can help me with this?
[SOLVED] Checkbox error check.
Moderator: General Moderators
Hi ya,
I would name each of your check boxes different in the question, that would help, then you can compare them IE
input type="checkbox" name=”do1” value= “1”>
input type="checkbox" name=”do2” value= “2">
etc...
then you could do something like an if statement if($do1 and $dont1) echo("Error");
Hope that is of some help
RedRasper
I would name each of your check boxes different in the question, that would help, then you can compare them IE
input type="checkbox" name=”do1” value= “1”>
input type="checkbox" name=”do2” value= “2">
etc...
then you could do something like an if statement if($do1 and $dont1) echo("Error");
Hope that is of some help
RedRasper
-
kingcharles
- Forum Newbie
- Posts: 1
- Joined: Mon Dec 01, 2003 9:21 am
What do you think of this?
Code: Select all
<?
php $count=count($dos);
for ($i=0; $i<$count; $i++) {
if ($dosї$i] && dontsї$i]){ Printf("error");} //checks if dos and donts
//with the same value are checked
}
?>
Last edited by kingcharles on Mon Dec 01, 2003 9:43 am, edited 1 time in total.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You could do what RedRasper suggests with the array too, so you could do:
and then
Using a loop you could make it more dynamic as well, so that you don't have to do a separate if test for each possible choice.
Mac
Code: Select all
<input type="checkbox" name="dosї1]" value= "sit">
<!-- and -->
<input type="checkbox" name="dontsї1]" value= "sit">Code: Select all
if (isset($dos[1]) && isset($donts[1])) {
// error
}Mac