0

Having read many posts I can't find a solution to my issue, can you help please.

I am working on a scripts that has a FORM which contains dynamically generated checkbox's

When a checkbox is selected the form posts the result as "0". That work fine but if the checkbox is not selected I need the form post result to be "1".

My code: The checkbox's are dynamically generated from a MySQL query and can be any number of checkbox's depending on how many rows are returned by the query.

<?php do { ?>
<tr>
   <td width="13%" class="seqid"><?php echo $row_TQ01['SeqID']; ?></td>
   <td colspan="2" align="left" class="audititems"><?php echo $row_TQ01['SeqText']; ?></td>
   <td>
      <input  name="check[<?php echo $row_TQ01['SeqID']; ?>]0" type="checkbox" class="checkbox" value="0" id="check0"/>
   </td>
</tr>
<?php } while ($row_TQ01 = mysql_fetch_assoc($TQ01)); ?>

If I select the checkbox reresults is:

Array ( [auditdate] => 2016-07-05 16:11 [HotelID] => NXLHR [AuditBy] => Demo [check] => Array ( [SeqID4901] => 0 [SeqID4902] => 0 [SeqID4903] => 0 ) [Comments] => [button] => Submit [MM_insert] => form1 [Room] => 0108 [clientID] => 1085 [SavedState] => 1 [SavedTableID] => 49 [UniqueID] => NXLHR01081467731479 ) 

If I do not select the check bos the result is:

Array ( [auditdate] => 2016-07-05 16:12 [HotelID] => NXLHR [AuditBy] => NaveX Demo [Comments] => [button] => Submit [MM_insert] => form1 [Room] => 0108 [clientID] => 1085 [SavedState] => 1 [SavedTableID] => 49 [UniqueID] => NXLHR01081467731479 ) 

Can anyone see a way of making the non select checkbox have a value of "1".

Many thanks in advance for your time.

DCJones
  • 3,121
  • 5
  • 31
  • 53
  • 2
    Unselected checkboxes are **not** passed through with the form, so no - you can't. Closest approximation would be to give the receiving PHP variables default values of `1` which are overridden with the `$_POST` data. – CD001 Jul 05 '16 at 15:22
  • You can use if(!isset($_POST['checkboxgeneratedname'])) which would be true if it were unchecked. You can also try something like this http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked – Moussa Khalil Jul 05 '16 at 15:27
  • 1
    You could always use hidden fields and set that to have a value of 1; then have actual checkboxes which, when clicked, change the value of the hidden field behind them. It's not ideal as someone could modify the values in source to make it look like it's been ticked when it hasn't, but most people won't a) know how to do that, and b) even bother with that. As a sanity check for that though, you could always see that ones with a value 0 have their corresponding check box submitted. – gabe3886 Jul 05 '16 at 15:31
  • you can use a ternary operator for this – Funk Forty Niner Jul 05 '16 at 15:40
  • Do you know how many fields you are submitting and what their name is? If so, I think that the hidden form field option @gabe3886 recommends is actually quite reasonable - however, the entire method of using the checkbox is a bit clumsy. – AnuragBabaresco Jul 05 '16 at 16:21
  • It's also probably worth noting that if you are going to have a lot of checkboxes and do go with my work around, PHP will only let you submit a certain number of fields in one go by default (around 1000), so you might get to a point where you know something is set but isn't showing. – gabe3886 Jul 06 '16 at 08:49

1 Answers1

0

I don't think you can change the HTML forms default value of checkboxes. However, when passing it in PHP why don't you try this

if($checkbox == 1)
{
  $checkbox = 0; 
}
else
{
  $checkbox = 1; 
}
Self Designs
  • 238
  • 4
  • 12