[SOLVED] Checkbox error check.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sanka
Forum Newbie
Posts: 2
Joined: Mon Dec 01, 2003 8:22 am

[SOLVED] Checkbox error check.

Post by sanka »

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?
RedRasper
Forum Commoner
Posts: 48
Joined: Thu Apr 24, 2003 6:36 am

Post by RedRasper »

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
kingcharles
Forum Newbie
Posts: 1
Joined: Mon Dec 01, 2003 9:21 am

Post by kingcharles »

What do you think of this?

Code: Select all

<? 
php $count=count($dos); 
for ($i=0; $i<$count; $i++) &#123; 
    if ($dos&#1111;$i] && donts&#1111;$i])&#123; Printf("error");&#125; //checks if dos and donts 
                                                   //with the same value are checked
&#125; 
?>
Last edited by kingcharles on Mon Dec 01, 2003 9:43 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You could do what RedRasper suggests with the array too, so you could do:

Code: Select all

&lt;input type="checkbox" name="dos&#1111;1]" value= "sit"&gt;
&lt;!-- and --&gt;
&lt;input type="checkbox" name="donts&#1111;1]" value= "sit"&gt;
and then

Code: Select all

if (isset($dos[1]) && isset($donts[1])) {
    // error
}
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
sanka
Forum Newbie
Posts: 2
Joined: Mon Dec 01, 2003 8:22 am

Post by sanka »

Sorry for the late reply. (I'm new here and I was expecting a mail notice)

Thanks for the help.
Combinding the replies on my question and puzzeling half a day more made it work.

Swiftly on my way to the next problem.
Post Reply