0

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!

10
  • 1
    You can't have an open PHP tag and echo inside a string you're printing. And echoing an array won't really work either. Commented May 14, 2018 at 16:44
  • Little confused. What does this look like when viewing rendered source? value='<?php echo($product) ?>. Commented May 14, 2018 at 16:45
  • 1
    @ficuscr I would assume it looks like value='<?php echo(Array()) ?> (with an array to string conversion notice.) Commented May 14, 2018 at 16:46
  • Can you show how you create the $product array, some example values, and an example of what you're trying to get out of it when the form is submitted? Commented May 14, 2018 at 16:48
  • 1
    Hi! Can you edit your question to show the output of var_dump($product), please? Commented May 14, 2018 at 16:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I agree! Although there is still the problem of the whole HTML section that contains $product being a string output with print
The "frame" stuff is throwing me off.
Yeah, same here, I'm not sure what that's referring to in this context.
Actually I need to divide the whole page into three sections: left section, top_right section and bottom_section, each being used to displayed different contents
@张剑桥 Don't focus on the frame stuff so much. Complicates it unnecessarily I think. With iFrames you can just as well think of it as 3 separate pages.
|

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.