2

So I researched online to see how I can pass an array which I POST through form.

Here's what I did.

 $itemsArr = array();
 while ($row = $result->fetch_assoc()) {
 $totalPrice += $row['item_price'];
 $totalItemsOnCart += $row['quantity'];
 $itemId = $row['item_idPK'];
 $itemName = $row['item_name'];
 $itemPrice = $row['item_price'];
 $itemQty = $row['quantity'];
 $itemsArr[] = array($userId, $itemId, $itemQty);
 echo "<tr>";
 echo "<td>".$itemId."</td>";
 echo "<td>".$itemName."</td>";
 echo "<td>".$itemPrice."</td>";
 echo "<td>".$itemQty."</td>";
 echo "</tr>"; 
 }

Then, I used the foreach loop to iterate through the multidimensional array.

echo "<form class = 'BreadCode' action='Function_Cart.php' method='POST'>";
foreach($itemsArr as $value){
 print_r($value);
 echo '<input type="hidden" name="itemsArr[]" value="'. $value. '">';
}
 echo "<input type='hidden' name='userId' value='$userId'>";
 echo "<input type='hidden' name='itemId' value='$itemId'>";
 echo "<input type='hidden' name='quantity' value='$itemQty'>";
 echo "<input class='PayButton' type='submit' name='Btn-pay' value='Pay'>";
 echo "</form>";

I keep getting an "Array to string conversion" error with or without the echo for the hidden input within the foreach() loop

I haven't really tried sending an array via form through POST method in the past. I just don't know how to go about this.

Below is the result of print_r()

Array ( [0] => 5112 [1] => 105 [2] => 2 ) 
Array ( [0] => 5112 [1] => 104 [2] => 1 )

Please help.

Thank you.

asked Dec 7, 2017 at 5:15
1
  • 1
    you can pass it asjson. before foreach <input type="hidden" name="itemsArr" value="'<?php echo json_encode($itemsArr ); ?>. '"> Commented Dec 7, 2017 at 5:18

2 Answers 2

2

You just need to encode array to json and decode it on action page

echo '<input type="hidden" name="itemsArr" value="'.json_encode($itemsArr). '">';

And at your action Page just decode it

$itemsArr = json_decode($_POST['itemsArr'],true)
answered Dec 7, 2017 at 5:35
2
  • Thanks. However, I have one problem when I decode it. It returns nothing when I try to echo var_dump($itemsArr) or print_r($itemsArr); Commented Dec 7, 2017 at 5:44
  • 1
    inspect element and check the input tag contain proper json data or not try to just print $_POST['itemsArr'] should it print something or note? Commented Dec 7, 2017 at 5:47
1

There is no need for place foreach and increase execution time. You can simply do that by using json_encode.

echo "<form class = 'BreadCode' action='Function_Cart.php' method='POST'>";
echo '<input type="hidden" name="itemsArr" value="' . json_encode($itemsArr) . '">';
echo "<input type='hidden' name='userId' value='$userId'>";
echo "<input type='hidden' name='itemId' value='$itemId'>";
echo "<input type='hidden' name='quantity' value='$itemQty'>";
echo "<input class='PayButton' type='submit' name='Btn-pay' value='Pay'>";
echo "</form>";
answered Dec 7, 2017 at 5:27
1
  • Thanks. There's just one problem. I can't seem to get the value when I try json_decode($_POST['itemsArr'],true); It returns nothing or null. Commented Dec 7, 2017 at 5:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.