473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 2219
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Dave Smithz | last post by:
Hi there, A PHP application I built has a section which lists a number of members to a club whose names each appear with a check box beside them that can be ticked. These check boxes are part...
5
by: Tongu? Yumruk | last post by:
I have a little proposal about type checking in python. I'll be glad if you read and comment on it. Sorry for my bad english (I'm not a native English speaker) A Little Stricter Typing in Python...
3
by: KathyB | last post by:
Hi, I'm trying to find a way to validate input text boxes where I don't know the names until the page is rendered. I've got 2 validate functions that fire with the onsubmit button of a "mini" form...
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
3
by: Rich | last post by:
I have a form with 2 check boxes. One of the check boxes is used to specify that the user is a "primary contact." When I check the primary contact box I want a second box for "standard contact"...
30
by: Michael B Allen | last post by:
I have a general purpose library that has a lot of checks for bad input parameters like: void * linkedlist_get(struct linkedlist *l, unsigned int idx) { if (l == NULL) { errno = EINVAL;...
2
by: Aaron Reimann | last post by:
I have a lot of check boxes. This is an update of the check boxes, I want something was checked, then to do an insert (which is currently working), if something is no longer checked...delete the...
3
by: IraqiAli | last post by:
ok i have a couple of textboxes. The user is required to input values from 1 to 6 into those boxes. and submit it, those values would then go into an array. What i need to do is do checks to make...
4
by: reinhout | last post by:
Hi everyone, I have made a script that checks if content is entered in the html boxes and if the required fields are empty, their border lights red. This all works, but if I try to make a...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.