Reputation: 67
I am having difficulties dealing with form that passes an array. I have included 5 variables ("$a,$b,$c,$d,$e") in an array called $product, then I passed it to another frame use form along with an input that requires user typing in a value. So there would be a array and an input being passed at the same time. So should I use "post" or "get"? It seems I should use "post" to pass the array, but the user input could only be passed via "get" right? Following is the code I had (the form is inside a table):
print "<td><form class=button action='bottom_right.php' method='post' target='bottom_right'>
<input type='text' name='quantity'>
<input type='hidden' name='product' value='<?php echo($product) ?>'>
<button type='submit' class='btn btn-primary mb-2'>Add</button>
</form></td>";
Besides, how could I retrieve the contents from another frame? Should that be something like below? How could I echo a single variable from the array?
<?php
session_start();
$quantity=$_POST['quantity'];
$product=$_POST['product'];
echo $quantity;
echo $product['a'];
echo $b;
?>
Looking forwards to your reply! Cheers!
Upvotes: 0
Views: 954
Reputation: 7054
Might be as simple as using explode/implode to transport the array.
<input type='hidden' name='product' value='<?= implode(',', $product) ?>' />
And then in PHP, you'll be looking for a string of concentated values under the product
key.
<?php
$quantity = (isset($_POST['quantity'])) $_POST['quantity'] ? : null;
$product = (isset($_POST['product'])) $_POST['product'] ? : '';
$productArray = explode(',', $product);
var_dump($productArray);
You could use POST or GET. You can convert the array to a string, using a delimiter to tokenize the parts, or you could use multiple hidden fields, one for each value in the array.
<!-- would probably POST if going this route...? -->
<input type='hidden' name='product[a]' value='<?= $product['a'] ?>' />
<input type='hidden' name='product[b]' value='<?= $product['b'] ?>' />
Play around with it and use var_dump($_POST);
to understand how the approaches modify what you receive.
Upvotes: 1