Click to See Complete Forum and Search --> : Php


eddy
01-20-2003, 08:55 AM
i have been trying to use session in php 4.2.3, i have used a very basic shopping cart, i have been unable to add things to the cart, it seems to work in earlier versions but not this one, does any one know why not

<?php
error_reporting(1); // suppress warning messages
session_start(); // Initialise session
session_register("trolleyContents"); // Register our variables
?>

<html><head><title>PHP session example</title>
</head><body>
<h2 align="center">Shopping trolley </h2>
<form method="post" action="SessionTrolley.php?<?=SID?>">
Choose an item: </p>
<input type="radio" name="choice" value="camera" /> camera<br />
<input type="radio" name="choice" value="snorkel" /> snorkel<br />
<input type="radio" name="choice" value="octopus" /> octopus<br />
<input type="radio" name="choice" value="cuddly toy" /> cuddly toy<br /><br />
<input type="submit" name="submit" value="Add to the trolley" />
<input type="submit" name="clear" value="Empty the trolley" /><br />
</form>

<?php
if ((!$trolleyContents && !$choice) || $clear) {
$trolleyContents = "";
echo "<p>Trolley currently empty</p>";
} else {
if ( $choice ) $trolleyContents = $trolleyContents . "<li>$choice</li>";
echo "<p>Trolley contains:</p>";
echo "<ul>$trolleyContents</ul>";
}
?>
</body></html>

pyro
01-20-2003, 11:53 AM
I've never done anything with sessions, so I can't help you. Your best bet would be to ask your question here... http://www.phpbuilder.com/board/

Brad
01-22-2003, 09:42 PM
Hello,

You are actually using the old method of sessions to accomplish what you are doing. After php 4.1 it became a lot easier to do sessions. I have a tutorial sort of written on it

http://www.php-scripter.com/php-tutorials/sessions/

A word of warning if anyone should follow the link the site itself (and hence the page the article is on) is in the early stages of development/design but for that tutorial the gyst of it is there I think.


<?php
error_reporting(1); // suppress warning messages
session_start(); // Initialise session
?>

<html><head><title>PHP session example</title>
</head><body>
<h2 align="center">Shopping trolley </h2>
<form method="post" action="SessionTrolley.php?<?=SID?>">
Choose an item: </p>
<input type="radio" name="choice" value="camera" /> camera<br />
<input type="radio" name="choice" value="snorkel" /> snorkel<br />
<input type="radio" name="choice" value="octopus" /> octopus<br />
<input type="radio" name="choice" value="cuddly toy" /> cuddly toy<br /><br />
<input type="submit" name="submit" value="Add to the trolley" />
<input type="submit" name="clear" value="Empty the trolley" /><br />
</form>

<?php
if ((!$_SESSION['trolleyContents'] && !$_POST['choice']) || $clear) {
$_SESSION['trolleyContents'] = "";
echo "<p>Trolley currently empty</p>";
} else {
if ( $_POST['choice'] ) $_SESSION['trolleyContents'] = $_SESSION['trolleyContents'] . "<li>{$_POST['choice']}</li>";
echo "<p>Trolley contains:</p>";
echo "<ul>{$_SESSION['trolleyContents']}</ul>";
}
?>
</body></html>


Should work, but as a warning I typed in the differences by hand on the fly, so I may have made a mistake