Passing on arrays

Programming, for all ages and all languages.
Post Reply
Kon-Tiki

Passing on arrays

Post by Kon-Tiki »

Ok, this might be a bit of a dumb question, but I forgot how to do this in PHP. I got an array from checkboxes. When they're selected and a submit-button is pressed, it passes on the array just fine. I need to pass on that array one more time after that, through another submit-button, but it then ends up as the string "array", instead of the actual array. I placed it in an input, like this:

Code: Select all

<input type="hidden" name="filter_edit" value="' . $filter_edit . '" />
$filter_edit comes from this:

Code: Select all

 if($_POST["filter_edit"] != NULL) $filter_edit = $_POST["filter_edit"];
$_POST["filter_edit"] comes from the checkboxes, and works fine (and so does the variable $filter_edit that gets it passed on)
Kemp

Re:Passing on arrays

Post by Kemp »

If $filter_edit is an array then you're telling PHP to convert it to a string to concat with the string around it, and the conversion for an array may very well be something containing the word 'array' IIRC, rather than an actual string representation of the contents.
zloba

Re:Passing on arrays

Post by zloba »

You can do it this way:

Code: Select all

<?php
foreach($filter_edit as $value){ ?> 
<input type="hidden" name="filter_edit[]" value="' . $value . '" />
<?php
}
?>
(or use "$key=>$value ", if you're using meaningful keys for checkboxes)

that is, recreate the set of values that got submitted, one hidden per value, like it was with checkboxes.

also, the way you check for having filter_edit in $_POST isn't perfect - it would cause a warning if not present.

Code: Select all

if(array_key_exists("filter_edit", $_POST)){
    $filter_edit = $_POST["filter_edit"];
   // .. may want to make sure it's an array
}
Kon-Tiki

Re:Passing on arrays

Post by Kon-Tiki »

Thanks :) Figured that out a bit ago, too, but't is good to know it wasn't just some dribbling, but actually's the right way ;)
Post Reply