Zen Cart Logo
Forums / General Questions / Passing values between pages - Confused

Passing values between pages - Confused

Views: 1,023

Results 1 to 6 of 6
28 Jan 2014, 2:13 PM
#1
earthenware avatar

earthenware

New Zenner

Join Date:
Dec 2011
Posts:
44
Plugin Contributions:
0

Passing values between pages - Confused

Can anyone please help me to better understand how parameters are passed between pages?

The example I'm looking at is adding a product to the shopping cart from a product page.

On the Product Page, the HTML form behind the “Add to Cart” button (as shown in View Source) shows the Action as:

<form name="cart_quantity" action="http://example.com/index.php?main_page=product_info&cPath=0&products_id=34&action=add_product" method="post" enctype="multipart/form-data"><input type="hidden" name="securityToken" value="2d9dd751a811a5be15fe2f053a2ae3b9" /> 

This says three things to me:

  • The method for transferring data is POST
  • The 'action' value being passed is “add_product”
  • The products_id (for identifying the product) is being passed.
    I would therefore have expected a POST array to be picked up by a PHP script and used to progress the shopping cart process. However, I can't seem to work out how this happens.

My testing seems to show that the array of data is received in 'init_cart_handler.php', specifically in the “if (isset($_GET['action'])) {” conditional loop. I say this because I traced the shopping cart functionality back through 'shopping_cart.php' to 'main_cart_actions.php' to 'init_cart_handler.php'.

But by the time the array has made it through to 'shopping_cart.php', it is being referred to as a POST array.

The reason I ask is because I am altering the visible URLs using .htaccess re-writes. According to Apache logs, an “un-rewritten” URL is received as follows:
blah...
Request:

"POST /index.php?main_page=product_info&cPath=0&products_id=34&action=add_product HTTP/1.1"

...blah...
Referrer:

"http://example.com/index.php?main_page=product_info&cPath=0&products_id=34" 

...blah

whereas the re-written URL is received as:

blah...
Request:

"POST /index.php?main_page=product_info&cPath=0&products_id=34&action=add_product HTTP/1.1"

...blah...
Referrer:

"http://example.com/A-product-name" 

...blah

The Request is the same. Only the referrer is different, yet one successfully initiates a purchase and the other just reloads the same page. This is where I'm confused. With no difference in the POST string being received by the server, why are the two requests being treated differently?

Can anyone help me to understand the mechanics here please?

PS:
v1.5.1
No mods
No Apache errors
No “myDEBUG...” errors.

28 Jan 2014, 3:06 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,100
Plugin Contributions:
56

Re: Passing values between pages - Confused

Earthenware:

Can anyone please help me to better understand how parameters are passed between pages?

The example I'm looking at is adding a product to the shopping cart from a product page.

On the Product Page, the HTML form behind the “Add to Cart” button (as shown in View Source) shows the Action as:

<form name="cart_quantity" action="http://example.com/index.php?main_page=product_info&cPath=0&products_id=34&action=add_product" method="post" enctype="multipart/form-data"><input type="hidden" name="securityToken" value="2d9dd751a811a5be15fe2f053a2ae3b9" /> ``` > > This says three things to me: > - > - The method for transferring data is POST > - The 'action' value being passed is “add_product” > - The products_id (for identifying the product) is being passed. > > I would therefore have expected a POST array to be picked up by a PHP script and used to progress the shopping cart process. However, I can't seem to work out how this happens. > > My testing seems to show that the array of data is received in 'init_cart_handler.php', specifically in the “if (isset($_GET['action'])) {” conditional loop. I say this because I traced the shopping cart functionality back through 'shopping_cart.php' to 'main_cart_actions.php' to 'init_cart_handler.php'. > > But by the time the array has made it through to 'shopping_cart.php', it is being referred to as a POST array. ... Like you've found, the variable that starts a cart-processing action is $_GET['action'] which is first intercepted by /includes/init_includes/init_cart_handler.php. That script next loads any .php files present in the /includes/extra_cart_actions folder, where cart-related overrides can be coded. Next loaded is /includes/main_cart_actions.php, which routes specific values (like add_product) for the $_GET['action'] variable to their associated functions within the session's shoppingCart object.

When you inspect the HTML associated with the add-to-cart form, you'll see hidden input variables that contain the product_id and (optionally) attribute values; those are the POST variables that are processed by the shopping_cart class.

28 Jan 2014, 3:30 PM
#3
earthenware avatar

earthenware

New Zenner

Join Date:
Dec 2011
Posts:
44
Plugin Contributions:
0

Re: Passing values between pages - Confused

That's helpful, thanks.

I'm still a little confused though. Perhaps my knowledge of HTTP is lacking.

1/ Why is the content of a method="post" received by a $_GET? I didn't think the two methods were interchangeable and I would have expected to see a $_POST in init_cart_handler.php.

2/ I don't understand what difference this makes to the transmission of the parameters to the next script. The HTTP request is identical in both examples I gave, only the referrer is different.

Apologies if I am missing somehting obvious.

28 Jan 2014, 5:05 PM
#4
lhungil avatar

lhungil

Totally Zenned

Join Date:
Feb 2012
Location:
mostly harmless
Posts:
1,818
Plugin Contributions:
4

Re: Passing values between pages - Confused

Earthenware:

... The reason I ask is because I am altering the visible URLs using .htaccess re-writes. ...
One cannot change the visible URLs (in HTML) using .htaccess rewrites... I hope you are not trying to "change" Zen Cart URLs by simply using redirects (via mod_rewrite) inside a .htaccess file. If you are this will cause you lots and lots of headaches...

The better way of altering the Zen Cart URLs is to change the way Zen Cart builds the URL for display. This results in the correct URL being placed directly in the generated HTML. CEON URI Mapping, Ultimate URLs, and Simple SEO URLs all use this method (along with actual rewrites - not redirects).

28 Jan 2014, 5:07 PM
#5
earthenware avatar

earthenware

New Zenner

Join Date:
Dec 2011
Posts:
44
Plugin Contributions:
0

Re: Passing values between pages - Confused

Ok, I've tried to work this out and what I think is happening is this:

  • A request to add a product to a cart is received by init_cart_handler.php.
  • If there is a parameter $_GET['action'], init_cart_handler.php calls main_cart_actions.php.
  • Main_cart_actions.php evaluates the $_GET['action'] and if the value is 'add_product', it calls the actionAddProduct' function, which is contained in shopping_cart.php.
  • Once shopping_cart.php is dealing with the request, it used the $_POST array.
    Assuming that I'm not talking nonsense, this would explain why a URL re-write that doesn't contain “action=add_product” (or something similar, I haven't tested it) doesn't work, despite the Apache access log recording a correct POST string.

Can anyone confirm please?

28 Jan 2014, 5:13 PM
#6
earthenware avatar

earthenware

New Zenner

Join Date:
Dec 2011
Posts:
44
Plugin Contributions:
0

Re: Passing values between pages - Confused

lhungil:

If you are this will cause you lots and lots of headaches...Yes, I can see why now.