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)); } } ?>




