Checkbox form validation...

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
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Checkbox form validation...

Post 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.
ryos
Forum Newbie
Posts: 16
Joined: Tue Feb 14, 2006 4:55 pm

Post 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'].
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

you got it backwards :P

Code: Select all

if(isset($_POST['name']){
   // do stuiff
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're missing a closing paren on that if. That might affect it.

and I was missing an 'n' in missing. :roll:
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post 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
SKDevelopment
Forum Newbie
Posts: 13
Joined: Thu Jan 26, 2006 10:42 am

Post 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
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

got a link to a live version we can look at?
kyoru
Forum Commoner
Posts: 26
Joined: Mon Feb 13, 2006 9:35 pm

Post 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
ahsan
Forum Newbie
Posts: 6
Joined: Fri Feb 24, 2006 7:22 pm

:-)

Post 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.
Post Reply