Pass Multi Values to PHP Variable and set this value to HTML input field

When you add [] to the end of an input name, that causes all the values of those inputs to be put into an array in PHP. So $_POST['checked_id'] is an array, not a string. Then when you do:

value="<?= $_POST['checked_id'] ?>"

you’re trying to echo the entire array into that one input. You can’t echo an array, so you get that warning.

If this is part of a loop, you need to index the array

for ($i = 0; $i < count($_POST['checked_id']); $i++) { ?>
    ...
    <input type="hidden" name="checked_id[]" value="<?=$_POST['checked_id'][$i]?>">
    ...
    <?php
}

Leave a Comment