Forums / General Questions / How come I can't modify $_POST variables in an observer

How come I can't modify $_POST variables in an observer

Locked

Views: 3,065

Results 1 to 7 of 7
This thread is locked. New replies are disabled.
7 Apr 2008, 9:49 PM
#1
audleman avatar

audleman

New Zenner

Join Date:
Jul 2007
Posts:
69
Plugin Contributions:
0

How come I can't modify $_POST variables in an observer

Hello,

I have a gift product that allows the user to enter a gift recipient address. I am writing a piece of code that sends the address to the USPS and replaces it with a scrubbed address. Technically, what I do when I get the new address is update the $_POST['id']['txt_XX'] variables.

Currently I have the code in includes/modules/pages/product_info/header.php and it works just fine. HOWEVER, when I try to move the code to the update function in an observer, it ceases to function in that any $_POST variable that I update fails to be passed to the cart.

Can anybody explain why modifying $_POST variables in an observer doesn't work?

Thank you,

Kevin

http://www.yesmagazine.org/store/index.php?main_page=product_info&products_id=16

8 Apr 2008, 12:53 AM
#2
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: How come I can't modify $_POST variables in an observer

Are you perhaps hooking your observer at a place where the POST variable contents have already been read and processed? ie: too late in the process?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 Apr 2008, 4:42 PM
#3
audleman avatar

audleman

New Zenner

Join Date:
Jul 2007
Posts:
69
Plugin Contributions:
0

Re: How come I can't modify $_POST variables in an observer

My observer is responding to NOTIFY_HEADER_END_PRODUCT_INFO. Definitely before it's being acted on.

I found this post at php.net: http://us.php.net/manual/en/language.variables.predefined.php#74620

The guy says "Dealing with "superglobals" and functions is not as straightforward as it may seem when you're doing manipulations. (follow the link for the rest of his post). "

He points out that superglobals can't necessarily be changed in functions, but doesn't say why or what to do about it. I was hoping somebody here might have some insight. Surely ZenCart users have attempted to modify $_POST variables in an observer before?

Kevin

8 Apr 2008, 4:59 PM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: How come I can't modify $_POST variables in an observer

What's in your observer class?
Are you sure it's firing?

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 Apr 2008, 5:15 PM
#5
audleman avatar

audleman

New Zenner

Join Date:
Jul 2007
Posts:
69
Plugin Contributions:
0

Re: How come I can't modify $_POST variables in an observer

Yes, it's definitely firing. What happens is I take a set of address fields, send them to the post office, and replace the $_POST variables with the response. Any $_POST variable that I touch comes up blank in the shopping cart. For testing purpose, I commented out code so that only some of the address fields are updated while others are left untouched. The ones I don't touch come through fine. The ones I touch come through blank.

Here's the relevant part of the code at the end of the notifier:

function update(&$class, $eventID, $paramArray) {

   //
   // Code omitted that sends address to USPS
   //

   //Update POST variables and call cart add function (will redirect to cart)
$_POST['id'][txt_7] = $Address;
//$_POST['id'][txt_8] = $Address2;
$_POST['id'][txt_9] = $City;
//$_POST['id'][txt_10] = $State;
$_POST['id'][txt_11] = $Zip;
		
global $goto, $parameters;
$_SESSION['cart']->actionAddProduct($goto, $parameters);

}

As you can see, address2 and state are commented out and they will show up in the shopping cart. Here's the relevant product if you want to see for yourself:

http://www.yesmagazine.org/store/index.php?main_page=product_info&products_id=70

For comparison, this other product does not trigger the notifier, instead it runs the same code placed in includes/modules/pages/product_info/header.php:

http://www.yesmagazine.org/store/index.php?main_page=product_info&products_id=16

All the $_POST address variables can be modified just fine in this case.

Kevin

8 Apr 2008, 6:40 PM
#6
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,513
Plugin Contributions:
176

Re: How come I can't modify $_POST variables in an observer

It doesn't make sense to me why you're doing any of this in /includes/modules/pages/product_info/header_php.php

When someone adds a product to their cart, under normal circumstances, they are taken to the shopping cart page ... which means that they leave the product_info page, meaning that /includes/modules/pages/product_info/header_php.php never fires.

And, if you've changed your cart to not go to the shopping cart page during add-to-cart, then even if you've got it changed to stay on the product_info page, that page's header_php.php is still the wrong place to be handling the submitted data.

I'm inclined to suggest that you put your add-to-cart validation code into a new file located at: /includes/extra_cart_actions/my_giftsub_address_validation.php

And in there, put all your validation code.

You can follow the programming logic in /includes/main_cart_actions.php (don't edit this file.) You'll see that the extra-cart-actions scripts can pre-process the data, and then when they're finished, the regular add-to-cart operations will happen normally.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

8 Apr 2008, 6:55 PM
#7
audleman avatar

audleman

New Zenner

Join Date:
Jul 2007
Posts:
69
Plugin Contributions:
0

Re: How come I can't modify $_POST variables in an observer

Good point. I had reworked the form to submit back to the product page out of ignorance of another way to do it. I'll look into reworking it to be more ZenCart standard and see if it helps.

Thanks for your input Dr. Byte!

Kevin