Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jose F. Garcia
Jose F. Garcia
22,949 Points

foreach <option> with paypal

I'm having some issues with paypal using foreach inside the select tag. When I use the paypal option tags with normal html (like the one commented out) everything works well and it adds the item to the cart. In the other hand, when I use the foreach loop to echo out the sizes along with the prices it shows well on the website, but then I get this error from paypal: "PayPal cannot process this transaction because of a problem with the seller's website. Please contact the seller directly to resolve this problem."

Here is how my array looks like:

$products = array();

$products[21101] = array(
    "name" => "Facial Wash",
    "img" => "img/img_detailed/facial-wash.png",
    "sizes" => array("30 ml 5.00 EUR", "200ml 25.00 EUR"),
    "paypal" => "XBSERUGPEXHDW"
);

Here is the paypal form:

<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
                        <input type="hidden" name="cmd" value="_s-xclick">
                        <input type="hidden" name="hosted_button_id" value="<?php echo $product["paypal"]; ?>">
                        <input type="hidden" name="item_name" value="<?php echo $product["name"]; ?>">
                        <table>
                            <tr>
                                <td>
                                    <input type="hidden" name="on0" value="Sizes">
                                    <label for="os0">Size</label>
                                </td>

                                <td>
                                    <select name="os0">
                                        <?php foreach($product["sizes"] as $size) { ?>
                                        <option value="<?php echo $size; ?>"><?php echo $size; ?></option>
                                        <?php } ?>
                                    <!--
                                    <option value="200ml">200ml 25.00 EUR</option>
                                    <option value="30ml">30ml </option>
                                    -->
                                    </select> 
                                </td>
                                <td>
                                    <input type="hidden" name="currency_code" value="EUR">
                                    <input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_cart_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
                                </td>   
                            </tr>
                        </table>
                    </form>

Remember, when I remove the php code and use the commented out lines, it works great.

Thanks in advance for any help or input.

1 Answer

There are multiple points where things could've gone wrong to generate that error, so I can't tell you exactly where to look. To help you move along, the first step to debug a PHP-generated webpage is to view the page source via your browser's dev tool View Page Source option (safari is command+option+U) and see if the generated HTML is as expected.