Page 1 of 1

Checkbox form validation...

Posted: Fri Feb 24, 2006 1:33 am
by kyoru
Check box code...initially checked

Code: Select all

<input name="same_as_ship" type="checkbox" value="true" checked />
And when the form is read...

Code: Select all

$form_same_as_ship = $_POST['same_as_ship'];

Code: Select all

if (!isset($form_same_as_ship){
The code just reads $form_same_as_ship as blank/null and i can't really figure out why o_o any ideas?
i've tried to see if the value="true" didnt work, tried empty(), didnt work so i'm stumped.

Posted: Fri Feb 24, 2006 1:59 am
by ryos
Checkboxes aren't sent to the server if they aren't checked. So, the value can be anything you want it to be, and to check to see if a box was checked, you call isset on $_POST['name'].

Posted: Fri Feb 24, 2006 12:08 pm
by kyoru
okay i changed the code around to do this..

Code: Select all

if (isset($_POST['same_as_ship']){}
			else {
                            do stuff 
                               }
and it still doesn't work, the box is checked so it should be sending something :\
thanks for the help

Posted: Fri Feb 24, 2006 12:14 pm
by s.dot
you got it backwards :P

Code: Select all

if(isset($_POST['name']){
   // do stuiff
}

Posted: Fri Feb 24, 2006 12:28 pm
by kyoru
no it is right, I want it do do soemthing if it is not set, I also tried using a hidden field with the same name and it still does not work...i am stumped

Posted: Fri Feb 24, 2006 12:53 pm
by feyd
you're missing a closing paren on that if. That might affect it.

and I was missing an 'n' in missing. :roll:

Posted: Fri Feb 24, 2006 1:06 pm
by kyoru
sorry it is there...i just left it out just to show how i requested the variable...i'm not sure where to check

Posted: Fri Feb 24, 2006 5:47 pm
by SKDevelopment
Try to print the array $_POST to make sure that $_POST['same_as_ship'] is set when the checkbox is checked:

Code: Select all

echo "<pre>"; print_r($_POST); echo "</pre>";
If $_POST['same_as_ship'] does not exist, please make sure that your form method is set to POST. If it is set to GET, you should check $_GET['same_as_ship'] instead of $_POST['same_as_ship'].

--
Best Regards,
Sergey Korolev
www.skdevelopment.com

Posted: Sat Feb 25, 2006 3:53 pm
by kyoru
i tried that, so the error had to be the check box itself, after reviewing it nothing was apparently wrong, but when i checked the forms, there were some </form> stuff lying around still that caused the issue cheers.

Posted: Sat Feb 25, 2006 9:49 pm
by feyd
got a link to a live version we can look at?

Posted: Sun Feb 26, 2006 1:27 am
by kyoru
feyd wrote:got a link to a live version we can look at?
i fixed it , the form tags were causing the error :P

:-)

Posted: Sun Feb 26, 2006 3:20 pm
by ahsan
Ya, your Form's ACTION was set to 'GET', wasnt it?
It happens when you send GET request and try to
read the values using $_POST.