I am trying to set up a dynamic PayPal form for multiple items. However, whenever I try to dynamically set the item_name, item_number, amount and quantity for each item, the cart on the PayPal website breaks, and I cannot display any items:
In my foreach loop, I want to add the ID variable (rendered with PHP) to each hidden input field:
$paypal_form .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_cart">
<input type="hidden" name="upload" value="1">
<input type="hidden" name="business" value="merchant@XXXXXX.com">
<input type="hidden" name="return" value="http://XXXXXX.com/confirmation.php">
<input type="hidden" name="cancel_return" value="http://XXXXXX.com/confirmation.php">
<input type="hidden" name="notify_url" value="http://XXXXXX.com/confirmation.php">';
foreach ($_SESSION["shopping_cart"] as $each_item) {
$item_id = $each_item['item_id'];
$item_name = $each_item['item_name'];
$product_id = $each_item['product_id'];
$price = $each_item['price'];
$paypal_form .='<input type="hidden" name="item_name_'.$item_id.'" value="'.$item_name.'">
<input type="hidden" name="item_number_'.$item_id.'" value="'.$product_id.'">
<input type="hidden" name="amount_'.$item_id.'" value="'.$price.'">
<input type="hidden" name="quantity_'.$item_id.'" value="1">';
} // end foreach
$paypal_form .= '<input type="hidden" name="currency_code" value="EUR">
<input type="submit" value="Pay with PayPal" class="pay_button"/>
</form>';
It seems that PayPal does not accept the dynamic $item_id amendment to the hidden input names?
When I leave the input names untouched (like below), it works, but then I cannot dynamically render the input fields for multiple items:
<input type="hidden" name="item_name_1" value="'.$item_name.'">
<input type="hidden" name="item_number_1" value="'.$product_id.'">
<input type="hidden" name="amount_1" value="'.$price.'">
<input type="hidden" name="quantity_1" value="1">