Forums / All Other Contributions/Addons / creating a $_SESSION var w/ AJAX, and jQuery, but is disappearing on subsequent pages

creating a $_SESSION var w/ AJAX, and jQuery, but is disappearing on subsequent pages

Started by tcarden on 14 Feb 2013, 8:25 PM · Views: 1,052

Results 1 to 2 of 2
14 Feb 2013, 8:25 PM
#1
tcarden avatar

tcarden

Zen Follower

Join Date:
Oct 2012
Posts:
282
Plugin Contributions:
0

creating a $_SESSION var w/ AJAX, and jQuery, but is disappearing on subsequent pages

Yesterday i coded a form checkbox to set a PHP Session variable on tpl_shopping_cart_default.php. i used jQuery and Ajax to do so. below is the code.

the problem is that the variable $_SESSION['hm-agreement'], is disappearing on subsequent pages, like for ex: tpl_shopping_cart_default, or log-in etc. i do a var_dump($_SESSION) to check this.

in the code below i've even echoed out the the SESSION var in a callback function via a js alert and it is echoing back correctly.

$(document).ready(function(){
	$('input:checkbox').click(function(){
		if(this.checked) {
			state = 'checked';
		} else {
			state = 'unchecked';
		}
		$.get('pyro-agreement.php', {action:state}, function(data) {alert(data); })
	}); })

am hesitant to supply a link to the dev. site here but could do so privately.
i did a little study to test the code in a simpler environment and seems to be working as expected.

http://tomcarden.net.previewdns.com/php-ajax-session-var-study/intro.php

any help on this would be appreciated. i don't understand why the variable once created doesn't exist on sub-sequent pages. i've

14 Feb 2013, 8:37 PM
#2
drbyte avatar

drbyte

Sensei

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

Re: creating a $_SESSION var w/ AJAX, and jQuery, but is disappearing on subsequent pages

If your ajax script hasn't started the same session that your live page is using, then none of its variables will carry over.

Simpler would be to have your javascript set the HTML for the corresponding input field on your actual page, and then have that normal page (nothing to do with ajax) submit that field with all the rest of its submission data, and parse it just like all the other input fields on the page, and have it set the session variable from there.
example: the checkout-comments field or the checkout terms-and-conditions field.

.
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.