how to read un-checked checkbox values in PHP?

SilverNodashi

Expert Member
Joined
Oct 12, 2007
Messages
3,340
Reaction score
48
Location
Johannesburg, South Africa
Hi everyone,

I need to capture dynamic form data in a PHP script, and would like to know how to capture un-checked checkboxes as well? Basically I have a list of usernames, each with a checkbox next to it. An admin person then needs to tick the checkboc next to a name, when the user has met certain criteria (say for example, pupils attending class).

When the form is submitted, a piece of code will send an email to a designated recipient (for example The Dean) with a list of attendees. But at the same time I also want to indicate which names weren't selected.

i.e.

"The following students attended the session:
John
Bob
Ann
Alex
Peter

The following students didn't attend:
Frank
Paul
Joe"

Does this make sense?

The question is, how do I see which are unchecked, but from a list dynamically generated? i.e. when the teacher choose the list of candidates, she has a list of criteria to choose from, which then generates the list. So the list could have 2 names or 20 names.
 
You must know what was sent to you, so those not in the $_POST were not checked.
 
Code:
<?php
var_dump($_POST)?>

<form method="post">
<fieldset>
<legend>Attendees:</legend>

Mary
<input type="radio" name="mary" value="yes" /> Yes&nbsp;&nbsp; <input type="radio" name="mary" value="no" checked /> No
<br>
Bob
<input type="radio" name="bob" value="yes" /> Yes&nbsp;&nbsp; <input type="radio" name="bob" value="no" checked /> No
<br>
<input type=submit value="submit" />
</fieldset>
</form>

works for me...
 
Try looking at it from a different point of view.

You know what query was used to create the users on the form, right? So it's as simple as calling that query again, and checking the checked values against that query. All "unchecked" users will be those that are left.
 
Try looking at it from a different point of view.

You know what query was used to create the users on the form, right? So it's as simple as calling that query again, and checking the checked values against that query. All "unchecked" users will be those that are left.

This is probably the only way todo it, but I'm trying to avoid it. Once the list of users have been loaded, it needs to be re-submitted with the checkboxes and some extra info (like "why John wasn't here today, i.e. a sick note or something) and this gets send via email to a designated recipient, and submitted back to the DB. So I'm trying to avoid this process from getting to slow. Thus another DB query will add extra processing time to the script.
 
if you're worried about db load just cache the initial query array in the session vars.
 
This is probably the only way todo it, but I'm trying to avoid it. Once the list of users have been loaded, it needs to be re-submitted with the checkboxes and some extra info (like "why John wasn't here today, i.e. a sick note or something) and this gets send via email to a designated recipient, and submitted back to the DB. So I'm trying to avoid this process from getting to slow. Thus another DB query will add extra processing time to the script.
It won't add that much time to the script, and it's a sure fire way to validate input as well.
 
Top
Sign up to the MyBroadband newsletter
X