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 Build a Simple PHP Application Integrating with PayPal Adding Available Sizes

Barry Deck
Barry Deck
4,016 Points

I've broken my Paypal buttons.

When I attempted the extra credit for this unit, I broke my form. In its current state, the "Add to Cart" button no longer works. The extra credit assignment was:

  1. Add a new option to the form that users can select in addition to size, something like "Style" with options of "Short Sleeve" and "Long Sleeve."

  2. Add an input field or textarea to the form that users can specify a value, something like "Gift Message." Be sure that this value gets displayed in the PayPal Shopping Cart after the item has been added to the cart.

First, I added the following code to each item in my products.php:

,
    "style" => array("Short Sleeve","Long Sleeve")

...so, a typical item in my $products array now looks like this:

$products[101] = array(
"name" => "Logo Shirt, Red",
"img" => "img/shirts/shirt-101.jpg",
"price" => 18,
"paypal" => "3LNRBH5F9ZBLY",
"sizes" => array("Small","Medium","Large","X-Large"),
"style" => array("Short Sleeve","Long Sleeve")
);

After that, I edited my Paypal buttons to include "Style" and "Message" inputs. Then, I took pieces of the newly generated code from the buttons to update the code for the form in my shirt.php as follows:

                <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>
                        <th>
                            <input type="hidden" name="on0" value="Sizes">
                            <label>Size</label>
                        </th>
                        <td>
                            <select name="os0" id="os0">
                                <?php foreach($product["sizes"] as $size) { ?>
                                <option value="<?php echo $size; ?>"><?php echo $size; ?> </option>
                                <?php } ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <input type="hidden" name="on1" value="Style">
                            <label>Style</label>
                        </th>
                        <td>
                            <select name="os1" id="os1">
                                <?php foreach($product["style"] as $style) { ?>
                                <option value="<?php echo $style; ?>"><?php echo $style; ?> </option>
                                <?php } ?>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <th>
                            <input type="hidden" name="on2" value="Message">
                            <label>Message</label>
                        </th>
                        <td>
                            <input type="text" name="os2" maxlength="200">
                        </td>
                    </tr>
                    </table>
                    <input type="submit" value="Add to Cart" name="submit">
                </form>

This code worked after I had updated the form based on editing the first button, but after I edited all my Paypal buttons to reflect the new inputs, none of the buttons would work.

I'm a bit stumped. :-/

Thanks,

Barry

Barry Deck
Barry Deck
4,016 Points

Hi All.

I've just discovered it actually works as planned, I guess. All tests I had performed correctly added the items to my cart. It's just that my cart was open in another tab and while the buttons would add my items to the cart, the code does not open a new tab for the shopping cart, so it wasn't acknowledged in my UX flow.

Perhaps this will be addressed before the course is finished. It would be better if users could see a message that the item was added. I don't really know whether this is just how Paypal's cart works, or if there's a way to fix the site code so that users don't have the same UX that I just did.

What do you guys think?

Relieved,

Barry

2 Answers

Tom Nulty
Tom Nulty
2,017 Points

Hi,

I understand what you mean about the Paypal cart on this project, and how it links straight to the Paypal site without warning and that once the cart open in another tab and you try to add a shirt, it will add to the cart and not tell you. I haven't found an easy solution to this, it does seem like it's how that Paypal cart is made. To combat this you would have to make your own cart and only use Paypal as the checkout but I think that is a bit to advanced for what they were trying to teach.

An easy alternative would be to just show an alert when the button is pressed to tell the user that it has been added to the cart.

Tom.

Barry Deck
Barry Deck
4,016 Points

Tom,

You're right. Definitely too advanced, as Randy does mention in the course that we'll be using Paypal to persist the cart. An alert would be a good fix. I wonder how dicey it gets when the post action needs to not only open the Paypal cart, but also update the product page to display the alert.

bd

Tom Nulty
Tom Nulty
2,017 Points

Barry,

Yeah, I think it would be good if they did an extension to this project where they updated the site to have a local basket instead. It will just feel a lot more streamlined. I don't think it will be too bad actually! You can leave the Paypal button as it is and at the top of the page you can catch the post like:

if ($_SERVER["REQUEST_METHOD"] == "POST")
{

}

Since the only time that the page will be set to POST is when the add to cart button is pressed, this should work fine. And then you could have a simple echo statement saying it has been added to the cart. You could put it in some styling div boxes treehouse have provided to make it look better too. This is all of course just a simple work around and not a fix for the bad UX you had.

Tom.

Barry Deck
Barry Deck
4,016 Points

But doesn't

                <form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">

establish that this condition is always true?

: /

Tom Nulty
Tom Nulty
2,017 Points

Nope it doesn't. That code is only run when the form is submitted, this will be when the "add to cart" button is pressed. The request method will be null before this. So if you did a conditional like my statement above, the code inside the conditional will only be run when the "add to cart" button is pressed.

I hope I have helped.

Tom.