Jump to content

Store and add to session variable


sjones

Recommended Posts

Hello, I am working on a site and I need some guidance please. I have a form that I would like to store two values in a session. All I need is the product and the quantity. Now I get the product from the form fields – size, color, logo, and voltage using a switch/case. And I get the quantity from the form field quantity. I would like to store these values in a session variable and be able to go back and add to, delete, or edit the variable.

My goal is to be able to figure out what products and quantity the client needs. Then send email order to the warehouse for processing. I am having trouble storing the value and adding to it, whenever I go back and use the form the only output I get is the current values. It over writes the old data. If you want to visit what I have now you can see it at (http://www.rediheat.com/feb2008/order.php?f=single_pie) the only form working now is single_pie Any Help?

 

here is my code:

<?php include 'includes/header.inc';  ?>

<table width="887" border="0" align="center" cellpadding="0" cellspacing="0">

      <tr>

        <td valign="top"><img src="images/spacer.gif" width="880" height="1"></td>

        <td rowspan="2"><img src="images/spacer.gif" width="7" height="466"></td>

      </tr>

      <tr>

      <!-- This is where the ordering form will be placed -->

        <td valign="top" class="product_article" style="background-image:url(images/order_page_bg.jpg); background-repeat:no-repeat; ">

        <div style="padding-left:14px; padding-top:10px; ">

        <!-- This area is where the include files will be  -->

<?php 
            $allowed_inc = array('single_pie', 'multi_pie', 'food_service', 'element'); 
             
                         
            if (isset($_POST['single_pie_submit'])) { // THIS IS WHERE THE PRODUCTS WILL BE ADDED TO THE DATABASE 
            require_once ('connection/mysql_connect.php'); // Connect to the database. 
            $error_message = ""; 
             
               if ($size != "blank") { 
                  $size = $_POST['size'];   
               } else { 
                  $error_message .= "<p>You forgot to enter a size</p> "; 
               } 
               if ($color != "blank")  { 
                  $color = $_POST['color']; 
               } else { 
                  $error_message .= "<p>You forgot to choose a color!</p> "; 
               } 
               if ($logo!= "blank") { 
                  $logo = $_POST['logo']; 
               } else { 
                  $error_message .= "<p>You need to choose if you would like a logo on your bag </p> "; 
               } 
               if ($voltage!= "blank") { 
                  $voltage = $_POST['voltage']; 
               } else { 
                  $error_message .= "<p>You did not choose the electrical voltage that you require </p>"; 
               } 
               if ($quantity != "") { 
                  $quantity = $_POST['quantity']; 
               } else { 
                  $error_message .= "<p>You did not select a quantity </p>"; 
               } 
                            
               if (strlen($error_message) > 1) { // IF THERE WAS AN ERROR 
                  echo $error_message . "<p>Click <a href='javascript:history.back()'>here</a> to go back</p>"; 
                  echo "</div>          
                           </td> 
                        </tr> 
                     </table>"; 
                  include 'includes/footer.inc'; 
                  exit(); 
               } else { // WE NEED TO PROCESS THE ORDER NOW 
                  if ($size && $color && $logo && $voltage && $quantity) { // IF ALL VARIABLES ARE TRUE 
                  ############################## 
                  ##      SWITCH / CASE      ### 
                  ############################## 
                                                       
                  $product = $size . $color . $logo . $voltage; 
                  switch ($product) { 
                     case '16redno110'; 
                        $prod='HP101'; 
                        break; 
                     case '16redno220'; 
                        $prod='HP105'; 
                        break; 
                     case '16redyes110'; 
                        $prod='HP103'; 
                        break; 
                     case '16redyes220'; 
                        $prod='HP108'; 
                        break; 
                     case '16blackno110'; 
                        $prod='HP113'; 
                        break; 
                     case '16blackno220'; 
                        $prod='HP115'; 
                        break;    
                     case '16blackyes110'; 
                        $prod='HP114'; 
                        break; 
                     case '16blackyes220'; 
                        $prod='HP116'; 
                        break; 
                     case '16greenno110'; 
                        $prod='HP119'; 
                        break; 
                     case '16greenyes110'; 
                        $prod='HP120'; 
                        break; 
                     case '16greenno220'; 
                        $prod='HP000'; 
                        break; 
                     case '16greenyes220'; 
                        $prod='HP000'; 
                        break; 
                      
                     case '18redno110'; 
                        $prod='HP102'; 
                        break; 
                     case '18redno220'; 
                        $prod='HP106'; 
                        break; 
                     case '18redyes110'; 
                        $prod='HP104'; 
                        break; 
                     case '18redyes220'; 
                        $prod='HP109'; 
                        break; 
                     case '18blackno110'; 
                        $prod='HP112'; 
                        break; 
                     case '18blackno220'; 
                        $prod='HP117'; 
                        break; 
                     case '18blackyes110'; 
                        $prod='HP110'; 
                        break; 
                     case '18blackyes220'; 
                        $prod='HP118'; 
                        break; 
                         
                     case '18greenno110'; 
                        $prod='HP121'; 
                        break; 
                     case '18greenyes110'; 
                        $prod='HP122'; 
                        break; 
                     case '18greenno220'; 
                        $prod='HP000'; 
                        break; 
                     case '18greenyes220'; 
                        $prod='HP000'; 
                        break; 
                     } 
                  ############################## 
                  ##   END OF SWITCH / CASE   ## 
                  ############################## 
                   
                  $cart_prod = $prod. "," .$quantity. "-" ; 
                   
                  session_register('cart_prod'); 
                  echo $cart_prod;                   
                  echo "   </div>          
                           </td> 
                        </tr> 
                     </table>"; 
                  include 'includes/footer.inc'; 
                  exit(); 
                  } 
               } 
            } 
             
            if (isset($_POST['multi_pie_submit'])) { // THIS IS WHERE THE PRODUCTS WILL BE ADDED TO THE DATABASE 
               echo "Multi pie submit button was set 
                     </div>          
                        </td> 
                     </tr> 
                  </table>"; 
               include 'includes/footer.inc'; 
               exit(); 
            } 
             
            if (isset($_POST['food_service_submit'])) { // THIS IS WHERE THE PRODUCTS WILL BE ADDED TO THE DATABASE 
               echo "Food service submit button was set 
                     </div>          
                        </td> 
                     </tr> 
                  </table>"; 
               include 'includes/footer.inc'; 
               exit(); 
            } 
             
            if (isset($_POST['element_submit'])) { // THIS IS WHERE THE PRODUCTS WILL BE ADDED TO THE DATABASE 
               echo "Element submit button was set 
                     </div>          
                        </td> 
                     </tr> 
                  </table>"; 
               include 'includes/footer.inc'; 
               exit(); 
            } 
          
               // page specified -> load content from relevant text file 
            if (isset($_GET['f']) and in_array($_GET['f'], $allowed_inc)) { 
                  include("includes/" . $_GET['f'] . ".inc"); 
          
               // no page specified -> load content from default.inc 
            } else { 
                  include("includes/products_default.inc"); 
            } 
         ?> 

<!-- This area is the end of the include files --> 

        </div>

       

        </td>

      </tr>

  </table>

<?php include 'includes/footer.inc'; ?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/129615-store-and-add-to-session-variable/
Share on other sites

The other user had pointed out that I did not put the PHP in tags and that it was hard to read. So I marked the other one as solved and posted the topic with the PHP in tags. I did not see a way to edit the original post or I would have put the tags around the original. If you know a way of editing the original, I would be happy to put that in quotes.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.