Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2005
    Posts
    15

    Unanswered: Remembering posted values (not sessions)

    I have 4 dropdown boxes. Quantity - Products - Sizes - Colors. After the user chooses they submit and the form action is the <?php $_SERVER['PHP_SELF']; ?>. At the top of the page is:

    <?php
    $product = $_POST['product'];
    $size = $_POST['select_2'];
    $color = $_POST['select_X'];
    $quantity = $_POST['quantity'];
    ?>

    Later on I simply echo these values. The user can submit a second product with options. When it does that, it replaces the first selection that I echoed. How do I tell it to remember the first selection, and add any additional inputs?
    Thanks!

  2. #2
    Join Date
    Feb 2004
    Posts
    533
    You can use Sessions to store variable state. You can create an array and assign it to the Session variable.

    http://www.php.net/manual/en/ref.session.php
    http://us2.php.net/manual/en/languag...s.external.php

    Some Examples
    Session adding array data
    Reading data from Session Array
    ~

    Bill

  3. #3
    Join Date
    Jul 2005
    Posts
    15

    Red face

    Ok, I'm looking at my PHP book. I can see how using an array over sessions would work, but I'm having a little trouble with the idea. Maybe someone can fill in the blanks.

    I have 4 input boxes for quantity-product-color-size. Once they are complete and submitted the info is placed in a table and the user can fill the boxes out again to add to the first selections. I can't seem to keep it from replacing the previous info.
    Code:
    <?php session_start();
    $product = $_POST['product'];
    $color = $_POST['color'];
    $size = $_POST['size'];
    $quantity = $_POST['quantity'];
    ?>
    Later on...
    <?php 
    $masterarr = array( $arr );
    $arr = array(  );
    array_push( $arr, $quantity, $product, $color, $size );
    ?>    
    <ol><?php foreach( $masterarr as $value){echo( "<li>$value</li>");}?></ol>
    I'm pretty new to this, and thats why the code above probably makes no sense. Here is what I wanted to do:
    Each time a user submits their specifications I want it to make a new array. First time is $arr1 (array should start at 1) second is $arr2 and so on. Each of these arrays has 4 places - quantity, product, color, size. That way I can easily find find the color of the 45th product $arr45[2] (example).

    From there I'll need to create a $masterarr of each $arr[i]. Then it will display each array as a seperate row.

    I'm really lost at this, can anyone help me?

  4. #4
    Join Date
    Feb 2004
    Posts
    533
    This works. Use a temp array to hold the session dat. Push a new product data array to the temp array, then save it back to the same session.
    posted test script
    PHP Code:
    <?php
    session_start
    ();

    // check for product selection
    if (isset($_POST[product])) {
        
    $products = array("product" => $_POST[product],
                
    "color" => $_POST[color],
                
    "size" => $_POST[size]);
    // initialize an array
        
    $tempproducts = array();

    // if session data set to $tempproducts array
        
    if ( !empty($_SESSION[productdat]) ) {
         
    $tempproducts =    unserialize($_SESSION[productdat]);
        }

        
    // add the new products array to the temparray
        
    array_push $tempproducts$products );

        
    // set the session productdat with the array
        
    $_SESSION[productdat] = serialize($tempproducts);
            
    print 
    "<p>Your products have been registered!</p>";
    }
    ?>
    Attached the full code. to test download and rename or copy to .php
    Attached Files Attached Files
    Last edited by savbill; 10-18-05 at 22:36.
    ~

    Bill

Posting Permissions

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