Sure.

Here it is.

There are three scripts.

session_1.php: the original form script.
session_1a.php: a revised script with a conditional else.
session_2.php: the receiving script for the session array variable.

Running script 1a and 2 works fine.

However the original script 1 and 2 gives me a error when returning to the
contents page the first time - it then fills the contents starting with the
number 2 on subsequent page loads.

as in:
1.
2. Hal 2000
3. Sonic Screwdriver

Try it and see and let me know.
Thank you.
TR
.............................


// this is the original script
// script session_1.php

<?php
session_start();
?>
<html>
<head>
<title>Storing an array with a session</title>
</head>
<body>
<h1>Product Choice Page</h1>
<?php
if (isset($_POST[form_products])) {
    if (!empty($_SESSION[products])) {
        $products = array_unique(
        array_merge(unserialize($_SESSION[products]),
$_POST[form_products]));
   }
$_SESSION[products] = serialize($products);
print "<p>Your products have been registered!</p>";
}
?>
<form method="POST" action="<?php $_SERVER[PHP_SELF] ?>">
<P>Select some products:<br>
<select name="form_products[]" multiple size=3>
<option>Sonic Screwdriver</option>
<option>Hal 2000</option>
<option>Tardis</option>
<option>ORAC</option>
<option>Transporter bracelet</option>
</select>
<br><br>
<input type="submit" value="choose">
</form>
<br><br>
<a href="session_2.php">content page</a>
</body>
</html>

...............
//this is a revised script with a conditional else
//script session_1a.php
<?php
session_start();
?>
<html>
<head>
<title>Storing an array with a session</title>
</head>
<body>
<h1>Product Choice Page</h1>

<?php
if (isset($_POST['form_products'])) {
    if (!empty($_SESSION['products'])) {
        $products = array_unique(
            array_merge(unserialize($_SESSION['products']),
            $_POST['form_products']));
    }else{ //this conditional has been added
        $products = $_POST['form_products'] ;
    }
    $_SESSION['products'] = serialize($products);
    print "<p>Your products have been registered!</p>";
}
?>

<form method="POST" action="<?php $_SERVER[PHP_SELF] ?>">
<P>Select some products:<br>
<select name="form_products[]" multiple size=3>
<option>Sonic Screwdriver</option>
<option>Hal 2000</option>
<option>Tardis</option>
<option>ORAC</option>
<option>Transporter bracelet</option>
</select>
<br><br>
<input type="submit" value="choose">
</form>
<br><br>
<a href="session_2.php">content page</a>
</body>
</html>
..............
//this is session_2.php

<?php
session_start();
?>
<html>
<head>
<title>Listing 16.5 Accessing session variables</title>
</head>
<body>
<h1> Content Page</h1>
<?php
if (isset($_SESSION[products])) {
   print "<b>Your cart:</b><ol>\n";
   foreach (unserialize($_SESSION[products]) as $p) {
       print "<li>$p";
   }
   print "</ol>";
}
?>
<a href="session_1.php">Back to product choice page</a>
</body>
</html>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to