Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Oct 2008
    Posts
    5
    Plugin Contributions
    0

    Default How do I make it remember custom form data in cart between logins?

    I'm trying to set up custom forms for each product in my store. I made two new database tables. One table, zen_forms, contains an HTML form for each product. (One field, "forms_fields", contains XML data that describes each form field. As you'll see in the code below, this field's contents are interpreted using SimpleXML.) The other table, zen_orders_forms, contains serialized user submitted data for each order of each product. When the customer adds a product to their cart, they're asked to fill out the associated form. We manage every form the customer fills out per session by serializing form $_POST data and storing it in an array, $forms, which in turn is serialized and stored in $_SESSION['forms']. Upon checkout, form data for each form in the order is entered into the database table zen_orders_forms.

    So far, everything works, as long as the user does not log out in the middle of the checkout process. If they do and log back in, their form input data is not restored, but of course, their shopping cart, and possibly other session information, is restored. What am I doing wrong?

    Below are the contents of the header_php.php file for the page where form data for the product being added to the cart is collected.

    Code:
    <?php
    	require(DIR_WS_MODULES . zen_get_module_directory('require_languages.php'));
    	
    	if(!isset($_GET['products_id'])) {
    		zen_redirect(zen_href_link(FILENAME_DEFAULT));
    	} else if($_SESSION['cart']->count_contents() <= 0) {
    		zen_redirect(zen_href_link(FILENAME_TIME_OUT));
    	} else if(!$_SESSION['customer_id']) {
    		$_SESSION['navigation']->set_snapshot();
    		zen_redirect(zen_href_link(FILENAME_LOGIN, '', 'SSL'));
    	} else if (zen_get_customer_validate_session($_SESSION['customer_id']) == false) {
    		$_SESSION['navigation']->set_snapshot();
    		zen_redirect(zen_href_link(FILENAME_LOGIN, '', 'SSL'));
    	} else {
    		$products_id = (int)$_GET['products_id'];
    		
    		$forms = isset($_SESSION['forms']) ? unserialize($_SESSION['forms']) : array();
    		$goto_cart = false;
    		if(isset($_POST) && !empty($_POST)) {
    			$forms[$products_id]['post'] = $_POST;
    			$goto_cart = true;
    		}
    		
    		if(isset($forms[$products_id])) {
    			$post = $forms[$products_id]['post'];
    			foreach($post as $key => $value) {
    				$_POST[$key] = $value;
    			}
    		} else {
    			$sql = "
    				SELECT f.products_id, f.forms_html, f.forms_fields, pd.products_name
    				FROM %s f
    				INNER JOIN %s pd
    				ON f.products_id=pd.products_id
    				WHERE f.products_id=%d";
    			$sql = sprintf($sql, TABLE_FORMS, TABLE_PRODUCTS_DESCRIPTION, $products_id);
    			$form_result = $db->Execute($sql);
    			
    			if(!empty($form_result->fields)) {
    				$forms[$products_id] = array(
    					'product_name' => $form_result->fields['products_name'],
    					'html' => $form_result->fields['forms_html'],
    					'fields' => $form_result->fields['forms_fields']);
    			}
    		}
    		
    		$_SESSION['forms'] = serialize($forms);
    		
    		$form_fields = @simplexml_load_string($forms[$products_id]['fields']);
    		
    		$form_html = $forms[$products_id]['html'];
    		foreach($form_fields as $field) {
    			$attrs = $field->attributes();
    			$name = (string)$attrs->name;
    			$default = isset($attrs->default) ? (string)$attrs->default : '';
    			$value = isset($_POST[$name]) ? $_POST[$name] : '';
    			
    			switch($attrs->type) {
    				case 'text':
    					$form_html = str_replace(sprintf(':%s:', $name), $value, $form_html);
    					break;
    				case 'checkbox':
    					$form_html = str_replace(sprintf(':%s:', $name), empty($value) ? '' : 'checked="checked"', $form_html);
    					break;
    				case 'radio':
    					$form_html = str_replace(sprintf(':%s:', $name), ($value == $default) ? 'checked="checked"' : '', $form_html);
    					break;
    				case 'select':
    					foreach($field->children() as $option) {
    						$option_value = (string)$option;
    						if($value == $option_value) {
    							$form_html = str_replace(sprintf(':%s:', $name), 'selected="selected"', $form_html);
    							break;
    						}
    					}
    					break;
    				case 'textarea':
    					$form_html = str_replace(sprintf(':%s:', $name), htmlspecialchars($value), $form_html);
    					break;
    			}
    		}
    		
    		if($goto_cart) {
    			zen_redirect(zen_href_link(FILENAME_SHOPPING_CART));
    		}
    	}
    ?>

  2. #2
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: How do I make it remember custom form data in cart between logins?

    Contents of the customer's shopping-cart (basket) are store in 2 database tables: customers_basket and customers_basket_attributes. That data is updated every time someone adds something to their cart, or adjusts its contents. If you've written custom code that bypasses the built-in handling of products and attributes, you'll have to expand your custom code to store that data if you want to be able to retrieve it between sessions.
    .

    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.

  3. #3
    Join Date
    Oct 2008
    Posts
    5
    Plugin Contributions
    0

    Default Re: How do I make it remember custom form data in cart between logins?

    Quote Originally Posted by DrByte View Post
    Contents of the customer's shopping-cart (basket) are store in 2 database tables: customers_basket and customers_basket_attributes. That data is updated every time someone adds something to their cart, or adjusts its contents. If you've written custom code that bypasses the built-in handling of products and attributes, you'll have to expand your custom code to store that data if you want to be able to retrieve it between sessions.
    Doesn't all session data get automatically stored in the database table zen_sessions?

  4. #4
    Join Date
    Jan 2004
    Posts
    66,443
    Plugin Contributions
    279

    Default Re: How do I make it remember custom form data in cart between logins?

    Sure, but sessions do expire, and definitely do not survive logouts.
    .

    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.

 

 

Similar Threads

  1. v153 Add custom form submission data access on Admin backend
    By fahadali in forum Customization from the Admin
    Replies: 9
    Last Post: 12 Feb 2016, 02:59 AM
  2. v1.2.x Can I create custom client logins w/custom products?
    By gdiddy1 in forum General Questions
    Replies: 4
    Last Post: 2 Nov 2015, 06:56 PM
  3. Trying to make a custom order form
    By Phil Roark in forum General Questions
    Replies: 14
    Last Post: 1 Dec 2010, 10:09 PM
  4. Can Zen Cart transmit form data?
    By haf4046 in forum Templates, Stylesheets, Page Layout
    Replies: 10
    Last Post: 10 Oct 2007, 08:40 PM
  5. How to Retain Form Data?
    By brycej2 in forum General Questions
    Replies: 3
    Last Post: 7 Sep 2007, 09:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
disjunctive-egg