I have been working on one single little problem for the past 10 hours. I have received help from multiple sources in multiple forums and nothing has worked completly. My page-of-anger is http://www.localheroclothing.com/step2y.php. Here is what the page needs to do:
Let the user choose a product, which brings up the sizes and colors available for that product. When they submit the product specification it is added above the table (PHP_SELF) and they may add another. But my php keeps replacing the old data with the new.

I have tried to change the JS, I tried to use hidden fields, and I'm about to throw my computer out the window. My site has taken me months and this is the last part I need to finish.

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

<table width="100%"  border="1" cellspacing="0" cellpadding="0">

<?php 
$rowsx=0+$inputrows; 
if(isset($_POST["rowsx"])){ 
$rowsx=$_POST["rowsx"]; 
} 
for($e=0;$e<=$rowsx;$e++){ 
?>

<tr>
<td><input type="text" value="<?php echo( $quantity .' - '. $product .' - '. $color .' - '. $size ); ?>" size="53">
</td>
</tr>

<?php 
} 
?>

</table>
<table><tr><td>
 <input name="quantity" type="text" size="2">
            </div></td>
            <td><div align="center">
              <span class="style2">Product</span>              
<select name="select_1" id="productdd" onChange="switch_select(); switcher_select();"> <option>-- Products --</option> <option value=12.99>T-Shirt</option> <option value=13.99>Longsleeve T-Shirt</option> <option value=19.99>Raglan Jersey Shirt</option> <option value=12.99>Woman's T-Shirt</option> <option value=14.99>Jr. Capsleeve</option> <option value=12.99>Jr. Tank Top</option> <option value=12.99>Jr. V-Neck</option> <option value=23.99>Hoodie</option> <option value=16.99>Sweatshirt</option> <option value=15.99>Trucker Hat</option> <option value=7.99>Mousepad</option> </select> </div></td> <td><div align="center"> <span class="style2">Color</span>
<select name="select_2" id="select_2" onchange="colors();"> <option>Please Select a Product</option> </select> </div></td> <td><div align="center"> <span class="style2">Size</span>
<select name="select_X" id="select_X" onchange="sizes();"> <option>Please Select a Product</option> </select> <input type="hidden" name="rowsx" value="<?php echo $rowsx+1; ?>" /> <input name="submit" type="submit" value="Add another" />

Please please please help me, I'm going insane. The options that the user chooses need to transfer to the next page (sessions). The name and id's of the inputs need to stay the same for the JS script. If anyone needs more info please let me know.

    Use a session array to hold all of your data so nothing gets overwritten. Just add on to the end of the array with every POST and extract the array when you need to display the contents.

      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.
      First the user chooses their product- 3-Tshirts-Medium-Green
      When they click the "sumbit" button I use the array_push() to add to the array. (i'm not sure how I'm going to seperate the 4 inputs and for each submitted row?)
      Then I write out the array above the input boxes with <ol>foreach( $arr as $value ) {echo( "<li>$value</li>");}</ol>

      I'm just confused on how I should set up the array to seperate the 4 inputs. I'l need to use their values later for determining the price. Could I make an array of arrays? $masterarr = array( $arr1 , $arr2, $arr3 ); Each $arr would be 1 product specification.

      Any ideas?

        This worked.
        Set a temporary array with the previous session data, the push the new array data to the temp array and save it back to the session. There's probrobly a shorter way of doing this.

        push data to session

        <?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 with the array
        	$_SESSION[productdat] = serialize($tempproducts);
        
        print "<p>Your products have been registered!</p>";
        }
        ?>
        

        Attached the full code. to test download and rename .txt to .php

          Wow thanks. I appreciate it.

          There's probrobly a shorter way of doing this.

          I had help from a different forum and this is what I have.

          <?php 
          session_start(); 
          if(isset($_POST['product'])) # or however you want to check that something is posted 
          { 
            $thisItem['qty'] = $_POST['quantity']; 
            $thisItem['product'] = $_POST['product']; 
            $thisItem['color'] = $_POST['color']; 
            $thisItem['size'] = $_POST['size']; 
            $_SESSION['items'][] = $thisItem; # add array to session 'items' array 
          } 
          ?> 
          
          When it needs to be displayed...
          
          <?php 
          if(isset($_SESSION['items']) and count($_SESSION['items']) > 0) 
          { 
            echo "<ol>\n"; 
            foreach($_SESSION['items'] as $ix => $val) # val will be an array 
            { 
              echo "<li>({$val['qty']}) - {$val['product']}, {$val['color']}, {$val['size']}</li>\n"; 
            } 
            echo "</ol>\n"; 
          } ?>

          I don't know if it's better, but it works and I'm happy. I do have a question about these type of arrays though, maybe you can help. First off, I don't know much about arrays. I kinda understand how to gat vars from arrays, but I'm not sure how to grab specific things like the quantity from product3 (example).
          The second thing is that I've read about deleting array vars in the front and back, but what if someone wants to remove a product from the middle? I'd like to have a "remove" button by each row, but is it possible to remove items from the middle?

          Anyway, thanks a lot for the code and help. I really appreciate it!

            //you can access the 3rd variable int he product array like so
            $val = $_SESSION['product'][3];
            echo $val;
            
            //you can remove array variables from anywhere like so
            unset($_SESSION['product'][3]);
            
            //hope that helps
            
            // :)

            [man]unset/man

              Write a Reply...