Checkbox error check.

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Sanka
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Dec 01, 2003 6:03 am
Contact:

Checkbox error check.

Postby Sanka » Mon Dec 01, 2003 6:15 am

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?

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

dos and don'ts...

Postby ruturajv » Mon Dec 01, 2003 11:55 pm

Please check this first

Code: Select all

$dos = $_POST['dos'];
$dontss = $_POST['donts'];

$i = 0;
for ($i=0;$i<count($dos);$i++) {
  if ($dos[$i] == $donts[$i]) {
    echo ("You cant check same things");
  } else {
    //do your code here
  }
}

Sanka
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Dec 01, 2003 6:03 am
Contact:

Postby Sanka » Wed Dec 03, 2003 2:29 am

Sorry for the late reaction.
(Kinda new to this site and I was expecting a mail notice)

I've tried what you proposed but it does not seem to do the trick.
Have any other idees?

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

typo err

Postby ruturajv » Wed Dec 03, 2003 3:13 am

$dontss = $_POST['donts'];

Code: Select all

$dos = $_POST['dos'];
$donts = $_POST['donts'];

$i = 0;
for ($i=0;$i<count($dos);$i++) {
  if ($dos[$i] == $donts[$i]) {
    echo ("You cant check same things");
  } else {
    //do your code here
  }
}

Sanka
New php-forum User
New php-forum User
Posts: 3
Joined: Mon Dec 01, 2003 6:03 am
Contact:

Postby Sanka » Thu Dec 04, 2003 3:07 am

I found an other way to make it work. It's longer but it has other advantages. Works for now.
I will try your code later to see if it works.

Thanks again for your time and effort

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

javascript

Postby ruturajv » Thu Dec 04, 2003 3:33 am

YOu can always use javascript...

Code: Select all

if (document.formname.elements['dos[]'][1].value ==document.formname.elements['donts[]'][1].value) {
 alert("You can't select both values");
}


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests