eoLithic
eoLithic

Reputation: 883

Send existing multidimensional array via POST

On my page I have a multidimensional array which I need to pass to another page, and from that page I'm going to send the array to controller. The array will be always hidden to simplify a form.
I searched a little but didn't find an answer to my question. So it looks like this

<form action="index.php?route=common/fittingForm" method="post">
    <?php foreach($products as $product) { ?>
        <input type="hidden" name="products[]" value="<?php echo $product; ?>" >
    <?php } ?>
    <input type="submit" value="Buy" class="buy_button">
</form>

And the page that gets the array looks like this

<?php $products = $_POST["products"]; ?>
<?php
    foreach($products as $product)
        echo $product['model'];
 ?>

And of course everything doesn't work. And I don't know why. Thank you for your attention.

Upvotes: 2

Views: 1731

Answers (1)

Mike
Mike

Reputation: 1231

Encode it to JSON before you send it :

<form action="index.php?route=common/fittingForm" method="post">
       <input type="hidden" name="products" value="<?php echo json_encode($products);?>">
    <input type="submit" value="Buy" class="buy_button">
</form>

and decode it :

<?php
$products = json_decode($_POST["products"]);
    foreach($products as $product)
        echo $product['model'];
?>

Upvotes: 2

Related Questions