NYCPHP Meetup

NYPHP.org

[nycphp-talk] Proper Form Processing Techniques

Gary Mort garyamort at gmail.com
Mon May 19 13:40:33 EDT 2014


On 05/18/2014 01:43 AM, Michael B Allen wrote:
> Hi All,
>
>
> My current technique seems a little hackish because I'm using the
> hidden form element "cmd" to modify the action. In practice it might
> seem purer to modify the action to call /purchase/remove or
> /purchase/update for use with modern routing on the server.

Whether you are adding an item, removing an item, adding a code, or 
whatnot - all those actions are based on "updating" the order.

There is no need to do "different" processing for each one, you can do 
all of that processing in one process.

IE: "removing" an item is equivalent to setting the item quantity to 0 
or less[just for that joker who decides to put -1000 in the quantity 
field and force a submit].   So for every item that is submitted on the 
form, you can check the quantities, if zero or negative then if the 
product id is in the order list, remove it.

If positive, then either add or update the quantity.

If an item currently in the cart was not submitted, leave it alone.

This allows for submission updates that only change a single item, 
delete items, add multiple items, etc.

Coupon codes work in the same manner.  If there is no coupon code 
submitted, then you use the coupon code currently stored in the 
session.  If a coupon code is added, then add it to the session.  If 
there is an existing coupon code and a new one was entered, decide what 
you want to do[ie you might allow multiple codes, you might not.... ]

Basically each of these steps can be encapsulated into your class and 
processed in one step:

Class cart

function updateItems(....$itemInfoList);
function updateCoupon(...$couponInfo);

function update()
{
// get the info submitted
$this->updateItems($itemList);
$this->updateCoupon($coupon);
....
}

Up to you how you want to handle error checking - check between each 
step, check at the end, etc.  If storing data into an SQL database, you 
can use transactions to undo updates on an error.


Technically, you really should use PUT instead of POST, since POST 
implies all the information is on the form, wheras PUT implies that your 
are only submitting changes.

Not relevant is DELETE.  For MOST "deletions" your really doing an 
update.  IE your not "deleting" an item from the cart, you are updating 
the cart and removing that item.   Even if you "delete" the entire cart, 
all visitors have a cart, what your really doing is "emptying" the cart.


More information about the talk mailing list