Forum Moderators: coopster

Message Too Old, No Replies

passing multiple session variables through form submissions

does my logic make sense?

         

bessington

6:25 am on Jul 5, 2005 (gmt 0)

10+ Year Member




I have posted about this before.. and have come up with a solution that somewhat pacifies what i am trying to do via php. but i'm stuck with a feeling that i may be missing something that may make my programming easier.

I am trying to pass session variables through many different pages when a user clicks a product to buy. (i am not using MYSQL). When this product is selected and posted as a session variable i want all product variables to be printed on a final 'checkout' page. please let me know if this makes sense to you all.

each product page is going to look something like this:

the code..

<?
session_start();
?>

<srong>This is Product One</strong>
<form action="" method="post">
<input type="hidden" name="product1" value="Product One has been selected">
<input type="submit" name="Submit" value="Buy Now!">
</form>

<?
if (isset($_POST['Submit'])) {
$_SESSION['product1'] = $_POST['product1'];

}
?>
Thank you for choosing <strong><? echo $_SESSION['product1'];?></strong>!<br />
Let's see what happens on the <a href="page3.php">next page.</a><br />

then on page3..
You have selected these products:
<? echo $_SESSION['product1'];?>
<? echo $_SESSION['product2'];?>
<? echo $_SESSION['product3'];?>

This works well - but I am not sure how to make this work so that the user can select multiple products on one page (or how to have multiple forms on one page that will post the variables correctly). Also, is there any easier way to print out all of these session variables, rather than having to go through and print out every single one I have defined? (there is going to be about 100 different products to choose from).

Thanks so much for your help!

jatar_k

4:44 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you might want to looking at making your array a little more complex and by doing so make your programming simpler.

Be careful of over complicating things though ;)

thoughts

$_SESSION['product1'];
$_SESSION['product2'];
$_SESSION['product3'];

problems I see with this. If they remove product2 and you are looping checking what is set you now have a gap so you will either have to re assign 3 to position 2 or you will have to find another way to loop.

why not have a single array in the session for products?
$_SESSION['products'][0];
$_SESSION['products'][1];
$_SESSION['products'][2];

then when it comes to working the array you can just worry about the products portion in case you need to store other info in the top level of the session.

Might not matter and you may be happy with the way you are doing it, just a thought.

for selecting multiple, you just put them all in the same form, with checkboxes beside or something, then when it's posted youo add each selected product's info to your session.

printing, well with the example you posted I would do something like

$counter = 1;
while(isset($_SESSION['product' . $counter])) {
echo $_SESSION['product1'];
$counter++;
}

with my example I would probably use a foreach on $_SESSION['products']

mcibor

6:50 pm on Jul 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And with selecting more products you can make checkboxes, or text fields to write amount in. And then get rid of empty:

Product1 bla bla bla; Amount:<input type="text" name="amount[]"><br>
Product2 ble ble ble; Amount:<input type="text" name="amount[]"><br>...
...
then if submitted
verify if amount is array of integers
foreach($_POST["amount"] as $key=>$number){
if((int)$number) $SESSION["amount"][$key] = $number;
}
This way if product 1 is 0 and product2 is 7 and product3 is 98 then only 2 and 3 products are set.

Just remember that there's no association.
Hope this helps
Michal Cibor