0

Possible Duplicate:
Submit an HTML form with empty checkboxes

I'm try to submit some data to the database. I'm trying to submit the value for a checkbox in the form to the database as follow:

<input type='checkbox' name='continue' <?php if ($_POST[continue] == TRUE)  echo "value='y' checked='checked'"; else  echo "value='n'"; ?> />

when I submit the data when the checkbox is checked, the value will be 'y'. but when the checkbox is checked, there is no data sent to the database.

Please advise.

Community
  • 1
  • 1
Jack
  • 258
  • 2
  • 7
  • 15

4 Answers4

2

Solution 1: (the better way)

<input type="checkbox" name="continue"<?php echo (isset($_POST['continue']) ? ' checked="checked"' : ''); ?> />

In your database (?) file:

$continue = isset($_POST['continue']) ? 'y' : 'n';

.

Solution 2: (not recommended)

<input type="hidden" name="continue" value="<?php echo isset($_POST['continue']) ? 'y' : 'n'; ?>" />
<input type="checkbox" name="continue_x"<?php echo (isset($_POST['continue']) ? ' checked="checked"' : ''); ?> onclick="document.getElementsByName('continue')[0].value=this.checked?'y':'n';" />
DragonWork
  • 2,415
  • 1
  • 18
  • 20
  • okay.. when I try to submit the form and box is not check, the value is 'n' but it is not sent to the database. but when I checked the box, the value is 'y' and it is sent to the database. – Jack Jun 17 '12 at 15:56
  • on the other hand, if I switch the value 'n' for checked and 'y' for unchecked, the value 'n' is sent to the database. – Jack Jun 17 '12 at 15:57
  • What do you mean by database? Please try to express yourself detailed. – DragonWork Jun 17 '12 at 16:16
1

First of all, when you are trying to validate the checkbox using PHP is not going to do the live check unlike JavaScript that you can do it right away without the need to interact with the server side.

So what were you trying to do I assume that you want to insert a data from the form to the database.

<input type='checkbox' name='continue' <?php if (isset($_POST[continue])) echo 
"checked='checked'"; ?> />

So what you are doing with the above code is to check using isset function if the checkbox is checked and you recall the form again the checkbox will still preserve the state and it will still be checked.

Now come to the part where you need to get the value to insert into your database.

if (isset($_POST[continue])) 
    $cont = 'y';
    else $s = 's';

You will need this before you are going to insert the value to the query and instead of doing $_POST[continue] now just use $cont as a value in the query and you will get the correct value.

Ali
  • 9,997
  • 20
  • 70
  • 105
0

Try this

<input type='checkbox' name='continue' <?php if ($_POST['continue']==TRUE) { echo "value='y' checked='checked'"; } else { echo "value='n'"; } ?> />
Hardik
  • 1,429
  • 2
  • 19
  • 37
0

Checked checkboxes are successful (and submit their values), unchecked checkboxes are not (and don't).

You need to give the checkbox a fixed value and then determine if it was checked or not based on the presence (or absence) of the data, not the specific value of data.

The value only matters when you have multiple checkboxes with the same name (e.g. for "Tick every mode of transport you use on a regular basis" type questions.)

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335