swissbeets Posted July 8, 2008 Share Posted July 8, 2008 I am trying to save the shirt size and then bring it back up in the shopping cart page, but i cannot get it to come up in the URL this is the function that displays the drop down menu, the array is ('S','M','L','XL') i cannot figure out what variable to use or how to use it function showOptionsDrop($array, $active, $echo=true){ $string = ''; foreach($array as $k => $v){ $s = ($active == $k)? ' selected="selected"' : ''; $string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n"; } if($echo) echo $string; else return $string; } this is my hyperlink to save this information cho "<a href=\"shoppingcart.php?prod=".urlencode($row['product_id'])."?&size=$string[$k]"."?&action=add\">Add to Shopping Cart"."</a>"; i will then make a GET and display the size in the shopping cart but my URL will not work do i have to do something witth the session? any help is greatly appreciated Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 8, 2008 Share Posted July 8, 2008 You have to get the value of the dropdown via javascript if you want to do it this way. You are better off just wrapping each "item" in a form. Make a hidden input field with the name prod and the value of $row['product_id'], you already have the select menu for size, and then just add another hidden input with name action and value of add. Make sense? Quote Link to comment Share on other sites More sharing options...
swissbeets Posted July 8, 2008 Author Share Posted July 8, 2008 kind of i do not know how to use a hidden field but can find out but as for the rest, product id, and action are already being passed to the next page, i have up to this far working but then realized i needed to put sizes in there any chance you could clarify please i am still very new but want to learn Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 8, 2008 Share Posted July 8, 2008 All the other values work fine because the values are already known while PHP is building the page. The size though, is not. JavaScript is messy and, if someone has it disabled, they can't buy something from your site.... Instead, do something like this: <?php while($row=mysql_fetch_assoc($result)){ ?> <form method="GET" action="shoppingcart.php"> <input type="hidden" name="prod" value="<?php echo urlencode($row['product_id']); ?>" /> <input type="hidden" name="action" value="add" /> <?php showOptionsDrop($array,'M'); ?> <input type="submit" value="Add to Shopping Cart" /> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
swissbeets Posted July 8, 2008 Author Share Posted July 8, 2008 does this go inside my first while loop? sorry i am very new to this ? this is my current while loop while($row = mysql_fetch_array($product_set)){ echo "<p>"; echo $row['product_name']."<br />"; echo "$".$row['product_price']."<br />"."<br />"; show_picture($row); echo "<br/>"; echo "Size: "; ?><p> <select name="size"> <?php $size = array('Select','S','M','L','XL'); showOptionsDrop($size); ?> </select> </p> <?php echo "<a href=\"shoppingcart.php?prod=".urlencode($row['product_id'])."?&size=['$size']"."?&action=add\">Add to Shopping Cart"."</a>"; } your saying i should make it a form with hidden values rather than even use a session? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted July 8, 2008 Share Posted July 8, 2008 nothing in your code here has anything to do with sessions...but what i'm saying, is the above code should be changed to: <?php while($row = mysql_fetch_array($product_set)){ ?> <form method="GET" action="shoppingcart.php"> <input type="hidden" name="prod" value="<?php echo $row['product_id']; ?>" /> <input type="hidden" name="action" value="add" /> <p> <?php echo $row['product_name']; ?><br /> $<?php echo $row['product_price']; ?><br /><br /> <?php show_picture($row); ?><br /> Size: <p> <select name="size"> <?php showOptions(array('Select','S','M','L','XL')); ?> </select> </p> <input type="submit" value="Add to Shopping Cart" /> </p> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
swissbeets Posted July 8, 2008 Author Share Posted July 8, 2008 ok i understand what your saying with this and did so, but it is only displaying the top product and and not displaying a submit button these variables will be POSTed and then i can easily GET them at a later date is this correct? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.