Jump to content

Update check box when a form is changed


TecTao

Recommended Posts

I'm trying to get some code to work that will update a field to show a checkbox either checked or not checked.

 

The field either uses a Y or N.  Y is checked, N not checked.  A code that I have tried displays the correct result for the box either checked or not checked. 

input type="checkbox" name="obcDisplay" value="Y" <?=($r['obcDisplay'] == 'Y') ? 'checked="checked"' : ''; ?>/>

But I want the ability to change when the form is submitted.  so if I bring up a page and the checkbox is checked (Y) and I submint, I want the database to update to N and the other way around.

Link to comment
Share on other sites

So basically, you want the checkbox to reflect whatever is in the database? If so, you just need to read in the checkbox value from the database prior to displaying the form. Then assign whatever the database contains to $r['obcDisplay'] and display the form. If a value isn't found in the database, you can set $r['obcDisplay'] to whatever you want the default to be.

Edited by cyberRobot
Link to comment
Share on other sites

As a side note: Instead of using a string value of 'Y' or 'N' that you then need to test, it is more efficient to use 1 or 0 since those can logically be interpreted as the Boolean TRUE/FALSE values.

 

 

<?=($r['obcDisplay']) ? 'checked="checked"' : ''; ?>
  • Like 1
Link to comment
Share on other sites

Using the 1 or 0 as the value would make more sense.

 

What I am having trouble understanding and making work would be, if 1 = checked and 0 = not checked.  And the field for a particular user in the user table is set to 0 which returns an unchecked checkbox, I want to be able to check the box, click submit and update the DB field obcDisplay to a 1, and back and forth.

<input type="checkbox" name="obcDisplay" value="<?=$r['obcDisplay'] ?>" <?=($r['obcDisplay']) ? 'checked="checked"' : ''; ?>/>

so I put the result from the query to display the content of the obcDisplay field in the value = and it does display the correnct 1 or 0, and if it's a 1 the box is checked, if it's a 0 the box is unchecked.  It's updating the table with the new 1 or 0 that I can't get my arms around.

Link to comment
Share on other sites

It's updating the table with the new 1 or 0 that I can't get my arms around.

 

I think the problem may be that you expect that the value in the POST data will tell you if it is checked or not. When you are dealing with a checkbox with a unique name, the value is typically irrelevant. The reason is that only CHECKED checkboxes are returned in the POST data. So, simply checking if the POST value for that field isset() is enough to tell you if it was checked or not.

 

 

$obcDisplay_PostValue = isset($_POST['obcDisplay']) ? 1 : 0;
//Insert this value into the DB.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.