471,337 Members | 1,083 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Checking check boxes from post

I have a group of five checkboxes that I need to check in a script after a
post-operaiton. I was using the code:

$drv1 = $_POST['driving_1']

but if the user left checkbox "driving_1" empty, i get a php error upon
processing. So, I looked in the manual and found the fuction empty(). So I
tried:

If (empty($drv1)) {
then do this
}

But it doesn't seem to be working. Actually, what I really want is if not
equal to empty, something like:

If (!empty($drv1)) {
then do this
}

Your help is, as always, greatly appreciated.
Jul 17 '05 #1
3 2136
Hi arenaTR!
I like using these functions:

function read_post( $param_name ) {
if( get_magic_quotes_gpc() ) {
return isset( $_POST[$param_name] ) ? stripslashes_array(
$_POST[$param_name] ) : null;
} else {
return isset( $_POST[$param_name] ) ? $_POST[$param_name] : null;
}
}

function stripslashes_array( $given ) {
return is_array( $given ) ? array_map( 'stripslashes_array', $given
) : stripslashes( $given );
}
The first one distinguishes between the absence of the parameter and a
parameter which is equal to ''. It also takes care of magic quotes, if
needed. The second function allows the built-in stripslashes to work on
arrays, if for instance, you pass something like checkboxes or a
multiple select.

If you want, you can name all of your five checkboxes like this:
<input type="checkbox" name="driving[]">

Then, just call read_post() like this:
$driving_values = read_post( 'driving' ); # an array of values

That way, you can later change the number of checkboxes without having
to change your PHP code.

Mike van Lammeren
arenaTR wrote:
I have a group of five checkboxes that I need to check in a script after a
post-operaiton. I was using the code:

$drv1 = $_POST['driving_1']

but if the user left checkbox "driving_1" empty, i get a php error upon
processing. So, I looked in the manual and found the fuction empty(). So I
tried:

If (empty($drv1)) {
then do this
}

But it doesn't seem to be working. Actually, what I really want is if not
equal to empty, something like:

If (!empty($drv1)) {
then do this
}

Your help is, as always, greatly appreciated.


Jul 17 '05 #2
On Tue, 9 Mar 2004 13:18:50 -0500, "arenaTR" <ar*****@comcast.net>
wrote:
I have a group of five checkboxes that I need to check in a script after a
post-operaiton. I was using the code:

$drv1 = $_POST['driving_1']

but if the user left checkbox "driving_1" empty, i get a php error upon
processing.


Checkboxes don't submit themselves if they are unchecked, so
$_POST["driving_1"] will not exist. In this case, use isset():

if ( isset($_POST["driving_1"]) )
{
// checkbox was checked, and therefore submitted.
}

Also, read Mike's post about naming your checkboxes driving[] for
easier processing.

--
David ( @priz.co.uk )
Jul 17 '05 #3
"arenaTR" <ar*****@comcast.net> wrote in message news:<LM********************@comcast.com>...
I have a group of five checkboxes that I need to check in a script after a
post-operaiton. I was using the code:

$drv1 = $_POST['driving_1']

but if the user left checkbox "driving_1" empty, i get a php error upon
processing. So, I looked in the manual and found the fuction empty(). So I
tried:

If (empty($drv1)) {
then do this
}

But it doesn't seem to be working. Actually, what I really want is if not
equal to empty, something like:

If (!empty($drv1)) {
then do this
}

Try using the isset() function.
It should return false if the variable isn't set.

br,
Krishs
http://asdf.lv/mag/
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Tongu? Yumruk | last post: by
2 posts views Thread by Edward | last post: by
30 posts views Thread by Michael B Allen | last post: by
2 posts views Thread by Aaron Reimann | last post: by
4 posts views Thread by reinhout | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.