0

lets say we have this:

echo '<form method="post">
        <div class="form-group">
            <table class="table table-bordered table-hover table-striped" style="width:auto">
              <tr>
                <td><label for="array">ARRAY_NAME</label></td>        
                <td>
                 <input type="checkbox" name="array[]" value="1" /> option1<br />
                 <input type="checkbox" name="array[]" value="2" /> option2
                </td>
              </tr>
              <tr>
                <td><label for="array2">ARRAY_NAME2</label></td>        
                <td>
                 <input type="checkbox" name="array2[]" value="1" /> option1<br />
                 <input type="checkbox" name="array2[]" value="2" /> option2
                </td>
              </tr>
              <tr>
                <td><label for="array3">ARRAY_NAME3</label></td>        
                <td>
                 <input type="checkbox" name="array3[]" value="1" /> option1<br />
                 <input type="checkbox" name="array3[]" value="2" /> option2
                </td>
              </tr>
           </table>
        </div>
        <button type="submit" name="submit" class="btn btn-success">Submit</button>
</form>';

I tried to implement this code: <?php echo (isset($_POST['array1[0]']) && $_POST['array1[0]'] == 1) ? "checked='checked'" : "" ?>

but it didn't work! It only works if you have name="array1" and name="array2". this way I'm thinking I can save multiple data in a parent array saved! Like this form[array1[],array2[],array3[]].

Can someone give me a solution because I'm stuck! Thanks in advance guys!!

2
  • imagine that sometimes you want both 2 values to be checked! Commented Apr 4, 2016 at 6:53
  • that's how you have to get the array from POST: $array2= $_POST['array2'], then get elements: $array2[0]... but NOTE THAT the array will have elements only for checked checkboxes, i.e you may have 10 checkbox tags, only 2nd 4th and last one are checked, so array2 will have only 3 items, not 10, it will be little tricky to use the array to updated checked-status of your checkboxes Commented Apr 4, 2016 at 7:41

2 Answers 2

0

You are trying to access the values incorrectly.

You can access a array posted like this:

echo $_POST['array1'][0];

php.net/$_POST See the first comment.

1
  • thank you!! it was driving me crazy the syntact!!! it works!!!!! thank you again!!!! Commented Apr 4, 2016 at 7:56
0

When you put square brackets in the name of form control, PHP will inflate that set of elements with similar names into an array.

You need to access:

$_POST['array1'][0]

not

$_POST['array1[0]'] // This is incorrect

(You also need to match the actual name of your control: Your HTML has array, array2 and array3 but not array1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.