Forum Moderators: coopster

Message Too Old, No Replies

post data for checkboxes and radio buttons php errors

post data for checkboxes and radio buttons php errors

         

drooh

4:57 am on Feb 20, 2010 (gmt 0)

10+ Year Member



Ok, this my be something obvious but in my form Ive got radio and checkbox inputs in my form along with text and textarea inputs.

on my processing side using php when I simply use

$name = $_POST['name'];

it works fine for text and textarea if the user does not fill in anything

but when I use that for radio and checkboxes if the user does not check the checkbox or radio button then php will show errors

so im wondering what is the best way to handle this or what are some options?

from what I know you can use something like this

if(isset($_POST['name'])){
$name = $_POST['name'];
}

but what about
if(!empty($_POST['name'])){
$name = $_POST['name'];
}

are there any other ways to handle this?

the error is
"Undefined index name"

Readie

5:20 am on Feb 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For checkboxes, I use this:

$check = $_POST['check'];
if(isset($check) && $check != "") {
$someValue = 'Blah';
}


I havn't actually had reason to use a radio button in PhP yet, but I should imagine it'll go something like this:

$rad = $_POST['rad'];
if(isset($rad) && $rad != ""){
switch($rad) {
case "valueOne":
$someValue = 'One';
break;
case "valueTwo":
$someValue = 'Two';
break;
}
}

rocknbil

7:51 pm on Feb 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Correct, an object can be set and still be empty, but if it's not set, you'll get an undefined index when checking for empty. :-) So check for isset.

$rad = $_POST['rad'];
if(isset($rad) && $rad != ""){ ...


The small error here is you will get an "undefined index" error if there are no radios checked (see "soapbox" below.) You want to check post:

$rad = isset($_POST['rad']))?$_POST['rad']:NULL;

if ($rad) { .......

but when I use that for radio and checkboxes if the user does not check the checkbox or radio button then php will show errors


Checkboxes are easy. Checked = isset, not checked it won't be set.

<soapbox>
I get this request from clients all the time: "can we make it so NO radio buttons are checked when it loads, so they have to select one?" This is an abuse of the radio button; one should always be checked by default. I usually manage to convince them that if they want to use this functionality, a checkbox or select is more appropriate.

Second, having "no radios checked" makes your job more difficult. If one is checked by default - which is how they are supposed to work - you can always count on it being set. It also makes your progressive Javascript validation more difficult, you now have to add JS to make sure at least one is checked, when you otherwise wouldn't.

If it's one of the situations where you need to make sure they made that choice without overlooking it, don't use a radio button.
</soapbox>

drooh

4:52 pm on Mar 25, 2010 (gmt 0)

10+ Year Member



I think this should be
$rad = isset($_POST['rad'])?$_POST['rad']:NULL;

you had extra )
$rad = isset($_POST['rad']))?$_POST['rad']:NULL;

THANKS FOR THE HELP

Matthew1980

9:49 pm on Mar 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there Drooh,

Maybe repeating what's been said but in this context this would be preferable:

$rad = (isset($_POST['rad']) ? strip_tags($_POST['rad']) : NULL );

Same difference but encased in parenthesis for clarity and essentially your evaluating the vars, so it's the product of the equation that you assign ;-p

Also just adding the strip_tags() for good measure...

Cheers,
MRb