I have one dynamic checkbox (can be ++) that needs to stay checked after user submit.

I already tried some tricks like using a hidden input before my checkbox code in HTML. Now I stuck in doing an isset(_POST) and the checkbox didn't stay checked.

Here's my HTML:

<input type="hidden" name="hidden_name[]" id="hidden_name0">                                                                

<input type="checkbox" name ="name[]" id="name0">

<label for="name">Name</label>

--------UPDATE-------- And in my PHP file, here's the code:

$valueName = array();
                    foreach($_GET['hidden_name'] as $value)

That code doesn't work :/

How to make the checkbox stays checked after user check it and submit the form? What should I write in PHP? Do I really need a hidden input before my checkbox?

2 Answers

Merioles On Best Solutions

isset might be returning false since the submitted value is NULL. I suggest adding a value='1' on the hidden input field -- or are you updating that field with the corresponding checkbox value?

Alternatively, you have some other options:

  1. Change the name for each dynamic checkbox to have an identifier.

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

    Where x could be your dynamic ID. Notice the addition of the PHP code and using a ternary operator, you can check the checkbox if the $_POST['name-x'] is set.

  2. Add a value to the checkbox.

    <input type="checkbox" name="name[]" id="name0" value="name0" />
    <input type="checkbox" name="name[]" id="name1" value="name1" />
    <input type="checkbox" name="name[]" id="name2" value="name2" />

    However, you need to match this value in your PHP code.

    if ( isset( $_POST['name'] ) ) {
        $values = array();
        foreach( $_POST['name'] as $value ) {
            array_push( $values, $value );

    Then you have to modify your checkbox again to have inline PHP.

    <input type="checkbox" name="name[]" id="name0" value="name0" <?php echo in_array( "name0", $values ) ? 'checked="checked"' : '' ?> />
    <input type="checkbox" name="name[]" id="name1" value="name1" <?php echo in_array( "name1", $values ) ? 'checked="checked"' : '' ?> />
    <input type="checkbox" name="name[]" id="name2" value="name2" <?php echo in_array( "name2", $values ) ? 'checked="checked"' : '' ?> />

You can also create a function to display these inline codes to make it much cleaner. HTH!

xbee On

i assume that you have a form that have some checkboxes and try to check which checkbox is checked. here is an example code

<form action="" method="post">
    <label for="name">Name 1</label>
    <input type="checkbox" name ="name[]" value="name1">

    <label for="name">Name 2</label>
    <input type="checkbox" name ="name[]" value="name2">

    <label for="name">Name 3</label>
    <input type="checkbox" name ="name[]" value="name3">

    <label for="name">Name 4</label>
    <input type="checkbox" name ="name[]" value="name4">

    <button type="submit" name="submit">Submit</button>

and the php code

        $valueName = array();
        foreach($_POST['name'] as $value){