How to remember checkbox input in PHP Forms

10,694

Solution 1

My first suggestion would be to use some client-side validation first. Maybe an AJAX call that performs the validation checks before continuing.

If that is not an option, then try this:

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

So if subscribe is = 1, then it should select the box for you.

Solution 2

For Example, consider the following code for checkbox :-

<label for="course">Course:</label>
PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />

Then, this would remember the checkbox of "PHP" if it is checked, even if the validation for the page fails and so on for "n" number of checkboxes as shown below:-

<label for="course">Course:</label>
        PHP<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"]) && in_array("PHP", $_POST["course"]))) {
echo "checked";
} ?> value="PHP" />
        HTML<input type="checkbox" name="course[]" id="course" <?php if     ((!empty($_POST["course"]) && in_array("HTML", $_POST["course"]))) {
echo "checked";
} ?> value="HTML" />
        CSS<input type="checkbox" name="course[]" id="course" <?php if ((!empty($_POST["course"])     && in_array("CSS", $_POST["course"]))) {
echo "checked";
} ?> value="CSS" />
        Javascript<input type="checkbox" name="course[]" id="course" <?php if     ((!empty($_POST["course"]) && in_array("Javascript", $_POST["course"]))) {
echo "checked";
} ?> value="Javascript" />

And most importantly, do not forget to declare the "course" variable as an array at the start of the code as shown below :-

$course = array();

Solution 3

I have been battling how to create sticky check box (that is able to remember checked items any time you visit the page). Originally, I get my values from a database table. This means that my check box value is entered to a column on my db table.

I created the following code and it works just fine. I did not want to go through that whole css and deep coding, so...

CODE IN PHP

$arrival = ""; //focus here.. down
if($row['new_arrival']==1) /*new_arrival is the name of a column on my table that keeps the value of check box*/
{$arrival="checked";}// $arrival is a variable
else
{$arrival="";};
echo $arrival;

<b><label for ="checkbox">New Arrival</label></b>&nbsp;&nbsp;&nbsp;
<input type="checkbox" name ="$new_arrival" value="on" '.$arrival.' /> &nbsp;(Tick box if product is new) <BR><BR>
Share:
10,694
Derek
Author by

Derek

Updated on June 26, 2022

Comments

  • Derek
    Derek almost 2 years

    For usability purposes I like to set up my form fields this way:

    <?php
    
    $username = $_POST['username'];
    $message  = $_POST['message'];
    
    ?>
    
    <input type="text" name="username" value="<?php echo $username; ?>" />
    
    <textarea name="message"><?php echo $message; ?></textarea>
    

    This way if the user fails validation, the form input he entered previously will still be there and there would be no need to start from scratch.

    My problem is I can't seem to keep check boxes selected with the option that the user had chosen before (when the page refreshes after validation fails). How to do this?

  • ThoKra
    ThoKra about 14 years
    It's checked="checked" not selected
  • St. John Johnson
    St. John Johnson about 14 years
    I have to ask why so many downvotes for this. It's a good answer, but just missing explanation and code tags. (use four spaces or `s to start code syntax)
  • St. John Johnson
    St. John Johnson about 14 years
    Fixed, sorry. I was thinking <option> tags.
  • Jorge Rodríguez
    Jorge Rodríguez about 14 years
    I think it's because I had to edit it a load of times. I just bashed something out quickly without previewing it and had to correct it about 3 times. >_<
  • Asaph
    Asaph about 14 years
    This will produce an array index undefined warning if the checkbox is unchecked.
  • St. John Johnson
    St. John Johnson about 14 years
    @Asaph, good point, I changed it to isset. If the value is sent via POST, it HAS to be 1 (meaning checked). Therefore, we can just check to see if the key exists.
  • St. John Johnson
    St. John Johnson about 14 years
    You can use isset instead, it's faster as it is a language construct and not a function. The only case where you would need array_key_exist instead of isset is if the value of the array at that key was "null" This will never happen in POST/checkbox data.
  • St. John Johnson
    St. John Johnson about 14 years
    -1 identical answer as mine. And checked is not XHTML valid. It needs to be checked="checked"
  • ThoKra
    ThoKra about 14 years
    @Johnson You know that you can use the value="" attribute with the checkbox too? So it doesn't have to be 1, but no matter what the value is, and if it's checked, then it's at least set . So the isset() is a good way to go
  • Randell
    Randell about 14 years
    Corrected checked for xhtml compliance. Disclaimer: I didn't bother with xhtml compliance in the first place since the answer is intended to answer the question directly. Identical would mean you explained what isset does, which you didn't.
  • ThoKra
    ThoKra about 14 years
    Why sanitizing when you're just checking if the value exists? No need.
  • Jeremy Young
    Jeremy Young almost 5 years
    that is a neat solution but you are not actually using an array $course, so there is no need to worry about declaring it!