Page 1 of 1

Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 7:12 am
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.

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 10:01 am
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")
}
 

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 10:21 am
by someberry
I don't see how that would help in regards to the problem, but thanks anyway.

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 10:58 am
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..

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 11:12 am
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.

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 11:21 am
by aditya2071990
*visualises a guilty kid being reprimanded by a teacher*

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 11:29 am
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.

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 12:22 pm
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.

Re: Detect unsubmitted checkboxes

Posted: Sun Oct 19, 2008 12:44 pm
by someberry
onion2k wrote:That would probably be the most sensible approach.
Sigh. Thanks :(