views:

3063

answers:

8

Hi

I have an HTML form - with PHP, I am sending the data of the form into a MySQL database. Some of the answers to the questions on the form have checkboxes. Obviously, the user does not have to tick all checkboxes for one question. I also want to make the other questions (including radio groups) optional.

However, if I submit the form with empty boxes, radio-groups etc, I received a long list of 'Undefined index' error messages for each of them.

How can I get around this? Thanks.

A: 

Can you post your code where you're getting the error message?

It's a bit hard to debug what you're doing without more information.

Ben Alpert
+11  A: 

Unchecked radio or checkbox elements are not submitted as they are not considered as successful. So you have to check if they are sent using the isset or empty function.

if (isset($_POST['checkbox'])) {
    // checkbox has been checked
}
Gumbo
And in the form you need to make sure that the checkbox elements have a value set as otherwise some user agents won't consider the checkbox successful either.
KayEss
+4  A: 

An unchecked checkbox doesn't get sent in the POST data. You should just check if it's empty:

if (empty($_POST['myCheckbox']))
     ....
else
     ....

In PHP empty() and isset() don't generate notices.

Greg
+6  A: 

I've used this technique from time to time:

<input type="hidden" name="the_checkbox" value="0" />
<input type="checkbox" name="the_checkbox" value="1" />
ceejayoz
Relying on the web browser honouring the ordering of the successful form elements on a page, and your server side scripting honouring it too is asking for trouble.
KayEss
It works in all browsers I've encountered. Doesn't seem likely that that'll change, either.
ceejayoz
You deserve a better rating. Let's do some research! From the HTML 4.01 specification see "17.13.3 Processing form data" Step two: Build a form data set. "A form data set is a sequence of control-name/current-value pairs...". A sequence implies a logical order (you can look that up anywhere) consequently what you purpose might even be purposed by the standard. Even though it's not without flaws I'm intrigued to start using this right away.
John Leidegren
My problem is a bit more complicated than the one suggested here, but your solution fits my needs perfectly fine.
John Leidegren
+1  A: 

Use this

$myvalue = (isset($_POST['checkbox']) ? $_POST['checkbox'] : 0;

Or substituting whatever your no value is for the 0

Cruachan
+1  A: 

To add to fmsf's code, when adding checkboxes I make them an array by having [] in the name

<FORM METHOD=POST ACTION="statistics.jsp?q=1&g=1">
    <input type="radio" name="gerais_radio" value="primeiras">Primeiras Consultas por medico<br/>
    <input type="radio" name="gerais_radio" value="salas">Consultas por Sala <br/>
    <input type="radio" name="gerais_radio" value="assistencia">Pacientes por assistencia<br/>
    <input type="checkbox" name="option[]" value="Option1">Option1<br/>
    <input type="checkbox" name="option[]" value="Option2">Option2<br/>
    <input type="checkbox" name="option[]" value="Option3">Option3<br/>
    <input type="submit" value="Ver">

+1  A: 

Here is a simple workaround using javascript:

before the form containing checkboxes is submitted, set the "off" ones to 0 and check them to make sure they submit. this works for checkbox arrays for example.

///// example //////

given a form with id="formId"

<form id="formId" onSubmit="return formSubmit('formId');" method="POST" action="yourAction.php">

<!--  your checkboxes here . for example: -->

<input type="checkbox" name="cb[]" value="1" >R
<input type="checkbox" name="cb[]" value="1" >G
<input type="checkbox" name="cb[]" value="1" >B

</form>
<?php


if($_POST['cb'][$i] == 0) {
    // empty
} elseif ($_POST['cb'][$i] == 1) {
    // checked
} else {
    // ????
}

?>


<script>

function formSubmit(formId){

var theForm = document.getElementById(formId); // get the form

var cb = theForm.getElementsByTagName('input'); // get the inputs

for(var i=0;i<cb.length;i++){ 
    if(cb[i].type=='checkbox' && !cb[i].checked)  // if this is an unchecked checkbox
    {
       cb[i].value = 0; // set the value to "off"
       cb[i].checked = true; // make sure it submits
    }
}

return true;

}

</script>
idf
A: 

Unchecked checkbox elements are not submitted as they are not considered as successful. So you have to check if they are sent using the isset or empty function.

Ragims