Forum Moderators: coopster

Message Too Old, No Replies

How is this form processing on postbacks?

         

AffiliateDreamer

8:28 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

Im looking at some oscommerce code here (php), not sure if this is a 100% php question though.

The form has 3 buttons: update cart, delete cart and checkout.

The update cart and delete cart are just input of type=image.

What is confusing me is how does the form know which button was clicked?

Code:

<form name="cart_quantity" action="https://www.example.com/shopping_cart.php/action/update_product" method="post">

<input type="image" src="includes/languages/english/images/buttons/button_update_cart.gif" border="0" alt="Update Cart" title=" Update Cart " name="update_cart">

<input type="image" src="includes/languages/english/images/buttons/button_empty_cart.gif" border="0" alt="Empty Cart" title=" Empty Cart " name="empty_cart" onclick="return confirmEmpty()">

<input type="image" src="includes/languages/english/images/buttons/button_checkout.gif" border="0" alt="Checkout" title=" Checkout " name="checkout">

Obviously the processing is going on the backend via PHP, but how does PHP know which button was clicked?

dreamcatcher

8:43 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Its the button names that would be the processing point.

if (isset($_POST['update_cart']))
{
// do something
}
if (isset($_POST['empty_cart']))
{
// do something
}
if (isset($_POST['checkout']))
{
// do something
}

Only one of these can be set if any button is clicked.

dc

AffiliateDreamer

10:50 pm on Aug 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



so that is just a boolean check then? true/false?

dreamcatcher

6:08 am on Sep 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I guess so. The post var for the button is only set when the button is clicked. Also, with images, there are usually x and y co-ordinates sent to, so to be sure it would work ok, you should check for that too. So, the post vars would be something like:

if (isset($_POST['update_cart']) ¦¦ isset($_POST['update_cart_x']))
{
// do something
}
if (isset($_POST['empty_cart']) ¦¦ isset($_POST['empty_cart_x']))
{
// do something
}
if (isset($_POST['checkout']) ¦¦ isset($_POST['checkout_x']))
{
// do something
}

For a better understanding and to see whats coming in on the post array, do the following:

echo '<pre>';
print_r($_POST);
echo '</pre>';

Its just an easy way of having multiple buttons.

dc