Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get PHP to work with checkboxes (MySQL backend)?

Status
Not open for further replies.

Apollo6

Technical User
Jan 27, 2000
418
0
0
US
I have a Mysql table that contains a column (Type=tinyint) which is used as boolean. I currently have a form which queries the database and returns back several text fields for each record. Each record also has the above mentioned field for 'Supervisor Approval'.

What syntax is needed to allow the user(supervisor) to 'Check' the checkbox and write the checkbox value to the database. In this case the value would be '-1' meaning 'yes'.

Likewise, if a record is pulled from the database that has already been approved, value = -1, how do I make the checkbox on the form actually be checked.

Any help is appreciated and let me know if further information is needed or code snippets.

Thanks
 

The ideal code for the html is as follows:
Code:
<html>
...
<input type="checkbox" name="superApproval" value="-1" checked="checked">
...
</html>
If you had read the value from the database and had it in a variable using php, then you could echo the checked value:
Code:
<? php
...
$approvalChk = -1;
...
$approvalValue = "";
if ($approvalChk == -1) $approvalValue = "checked";
?>
<html>
...
<input type="checkbox" name="superApproval" value="-1" checked="<? echo $approvalValue ?>">
...
</html>
If the checkbox is checked, then it will submit the checkbox name and value... if the checkbox is not checked, then not even the checkbox name will be sent.

Does this make any sense?

Cheers,
Jeff
 
Thanks for the reply...

I'm not sure why your are setting the following:

<input type="checkbox" name="superApproval" value="-1" checked="<? echo $approvalValue ?>">

I am able to set the $approvalValue variable to either "checked" or "unchecked" depending on what it reads from the database. I can echo this out on the page for confirmation.

I have narrowed down my query results to display only records having a value=0 in the approval field. At the end of each record/row displayed, there is an Approval link which takes that specific record and displays it in another page. This page has the checkbox on it but it is loading on the page being "checked". When it actually isn't. I am guessing because the above statement sets the value=-1.

Still looking at it.
 

I am guessing because the above statement sets the value=-1.
Most certainly... that is why I used "..." below the line. You said you could read the value of the checkbox from the database... and so you would then assign that value to the $approvalChk variable. If the variable contains the number -1, then we know to set the checkbox's checked value to checked.

Please note... if the checkbox is to be checked: check="checked" but if the checkbox is not to be checked, then check="". You are not setting the checkbox to be unchecked... you only set it if it is to be checked.

Cheers,
Jeff

 
Let's say you read the record. We assume the values are saved in an array called $row:
Code:
<?php

if ($row['fieldname'] == -1) {
    $checked = "checked";
} else {
    $checked = "";
}
?>
<input type="checkbox" value="-1" <?=$checked?>>

The following code by BabyJeffy will set the box to checked in either case:
Code:
<input type="checkbox" name="superApproval" value="-1" checked="<? echo $approvalValue ?>">
 
Got it to work like this...Not sure it is the cleanest way but it works as desired. Thanks for the input!!!

Set the variable to the value read from the database/array:
if ($approved == -1){
$approved = "on";
}
else if ($approved == 0) {
$approved = "off";
}

Set the value of the checkbox accordingly:
<input type="checkbox" name="approved" <?php if($approved == "on"){echo " CHECKED";}?>>
 

Check that you output correct HTML:

Code:
<input type="checkbox" name="approved" <?php if($approved == "on"){echo "checked=\"CHECKED\"";}?>>

That way it'll work on all browsers properly.

Cheers,
Jeff

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top