Detect unsubmitted checkboxes

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
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Detect unsubmitted checkboxes

Post by someberry »

Imagine a collection of checkboxes, such as:

Code: Select all

<form action="index.php" method="post">
<p>
<input type="checkbox" name="food[]" value="Piza" /><br />
<input type="checkbox" name="food[]" value="Steak" /><br />
<input type="checkbox" name="food[]" value="Chips" /><br />
<input type="checkbox" name="food[]" value="Pasta" /><br />
 
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
Users can create their forms however they wish so that they can collect data. However, I have run into a small problem whereby if the user were to go back and edit some information, if they selected "Piza" before but wanted to uncheck it and submit it with no checkboxes checked, the POST request will not contain the 'food' variable at all.

If you submit the form without checking any then PHP will not put the element in the POST array, so it would look like:

Code: Select all

Array (
    [submit] => Submit
)
What I would like is the ability to get:

Code: Select all

Array (
    [submit] => Submit,
    [food] => 
)
So that I can deal with is appropriately. As I understand, this is a limitation of HTML, so would anyone have any ideas on the best way to handle this?

Thanks.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: Detect unsubmitted checkboxes

Post by aditya2071990 »

Well, set up a foreach loop that goes through each of the array's elements, then use php's inbuilt empty() function to check for empty stuff...

Let's say your array is $op_array, then,

Code: Select all

function is_empty($input_array) {
foreach ($array as $element) {
if (!empty($element)) {
return false;
}else{
return true;
}
};
 
//whatever your array is, name is $op_array
$op_array = array();
 
if(is_empty($op_array)){
   $op_array() = array("food"=>"empty")
}
 
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: Detect unsubmitted checkboxes

Post by someberry »

I don't see how that would help in regards to the problem, but thanks anyway.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: Detect unsubmitted checkboxes

Post by aditya2071990 »

Oops sorry, I did not read your problem correctly...

Well, haha the problem looks pretty simple...

Code: Select all

 
 
if($op_array[] == $op_array["submit"]["submit"]){
 
    $op_array[] = array("submit"=>"submit","food"=>"") 
 
}
 
 
Didn't test it though, you may run up into a little syntax which can be corrected easily..
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Detect unsubmitted checkboxes

Post by onion2k »

aditya2071990 wrote:Oops sorry, I did not read your problem correctly...
You still haven't read it properly. Stop guessing and read the problem properly first. Only reply if you're sure you've understood it and you have something worthwhile replying with.

@Someberry ... you can't force HTML to send empty values. You've got a couple of options really... firstly, as I presume you sent the form to the user you should already know which values weren't submitted, so just loop through all of the fields that were sent (using the same code that generated them) and put any missing ones into an array. Alternatively, you could put all the values into the users session and compare the returned form to that. And lastly, you could switch to using radio buttons for 'yes' and 'no' that both have values set rather than checkboxes.
User avatar
aditya2071990
Forum Contributor
Posts: 106
Joined: Thu May 22, 2008 11:30 am
Location: Hyderabad, India
Contact:

Re: Detect unsubmitted checkboxes

Post by aditya2071990 »

*visualises a guilty kid being reprimanded by a teacher*
Last edited by aditya2071990 on Sun Oct 19, 2008 11:43 am, edited 1 time in total.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: Detect unsubmitted checkboxes

Post by someberry »

onion2k wrote:@Someberry ... you can't force HTML to send empty values.
Thanks, onion2k. Wasn't the answer I was hoping for, but oh well.
onion2k wrote:firstly, as I presume you sent the form to the user you should already know which values weren't submitted, so just loop through all of the fields that were sent (using the same code that generated them) and put any missing ones into an array.
Unfortunatly not; we are a kind of intermediary.
onion2k wrote:Alternatively, you could put all the values into the users session and compare the returned form to that. And lastly, you could switch to using radio buttons for 'yes' and 'no' that both have values set rather than checkboxes.
Again, unfortunatly I cannot do this either as a user taking the poll can come back in x days/weeks/months/years/decades/centries and recomplete it.

One idea I did have was to parse the HTML we recieve using regex for checkbox inputs names, but before I proceed with this I was just trying to think of a more... suitable method.

Thanks.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Detect unsubmitted checkboxes

Post by onion2k »

That would probably be the most sensible approach.

Another, if you could add some Javascript to the HTML, would be to scan through it for checkboxes (pretty easy with DOM coding) when the page loads using a clientside script, and dynamically add a hidden form field containing a comma separated list of all the possible values ... but that would fall over if the user had Javascript switched off.
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

Re: Detect unsubmitted checkboxes

Post by someberry »

onion2k wrote:That would probably be the most sensible approach.
Sigh. Thanks :(
Post Reply