2

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.

3
  • another page to me means a separate request. is your situation all within the same request? Commented Nov 12, 2013 at 0:35
  • 1
    Well, I pass the array to first page (1 request), the from the first page I send the array to controller. So two request. Sorry, if I didn't understand your question, my english isn't flawless. Commented Nov 12, 2013 at 0:38
  • well if it's 2 or more requests ie a redirect, then you have to store the array in somewhere like $_SESSION, db or a file. Commented Nov 12, 2013 at 2:05

1 Answer 1

2

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'];
?>
Sign up to request clarification or add additional context in comments.

2 Comments

I get an error after I press submit button "Warning: Invalid argument supplied for foreach() in C:\apache\localhost\www\webshop.kg\catalog\view\theme\default\template\common\fittingForm.tpl on line 3". Any idea how to resolve this?
OH sorry i forget to notice you to decode it before ... i have updated my answer

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.