ok i cannot seem to get the session variables to save across files :/ It is hard since the book im using is older, so it relied on register_globals being on :/
anyways here are the 2 files, named session_cart.php and session_cart_checkout.php, respectively
<?
session_start();
$inventory = array (
"001" => "Tooth Paste",
"002" => "Facial Tissue",
"003" => "Cotton Swabs",
"004" => "Shampoo",
"005" => "Conditioner",
"006" => "Deodorant"
);
function populate_cart() {
global $name, $email, $items;
$_SESSION['items'] = array(0,0,0,0,0,0);
$_SESSION['name'] = 0;
$_SESSION['email'] = 0;
}
function get_user_info() {
?>
<h3>Please provide us with the following before continuing:</h3>
<form action=session_cart.php method=post>
<p>Name: <input type="text" name="name">
<br>Email: <input type="text" name="email">
<p><input type="submit" name="infosubmit" value="submit">
</form>
<?
}
function shop() {
global $name, $email, $items, $inventory;
?>
<p>Shop Below:
<form action=session_cart.php method=post><p>
<?
$total = 0;
for($i = 0; $i < sizeof($inventory); $i++) {
?>
Item ID: <b><?=key($inventory);?></b> Description: <b><?=$inventory[key($inventory)]?></b>
Qty: <input type="text" name="Items_in[<?=$i?>]" value="<?=$items[$i]?>" size="2" maxlength="2"><br>
<?
next($inventory);
$total = $total + $items[$i];
}
?>
<p>You have <?=$total?> items in your cart.
<p><input type="submit" name="additems">
</form>
<?
if($total > 0) {
?>
<form action=session_cart_checkout.php>
<input type="submit" name="checkout" value="Check out!">
</form>
<?
}
}
if(isset($_POST['additems'])) {
$items = $_POST['Items_in'];
echo("<h3>Cart Updated!</h3>");
shop();
} elseif(!isset($_POST['name'])) {
get_user_info();
} elseif(isset($_POST['infosubmit'])) {
populate_cart();
shop();
} else {
shop();
}
?>
<?
session_start();
$inventory = array(
"001" => "Tooth Paste",
"002" => "Facial Tissue",
"003" => "Cotton Swabs",
"004" => "Shampoo",
"005" => "Conditioner",
"006" => "Deodorant"
);
if(!isset($_SESSION['name'])) {
?>
<h2>Sorry, you need to <a href=session_cart.php>add some items</a>before you can check out.
<?
} elseif(isset($finish)) {
?>
<h3>Order complete!<h3>
<?
session_unset();
session_destroy();
} else {
?>
<h2>Checkout</h3>
<p>Thank you for shopping with us <?=$_SESSION['name']?>, your order is below:
<p><table border=1>
<?
$total = 0;
for($i=0; $i<sizeof($inventory); $i++) {
?>
<tr><td>Item ID: <b><?=key($inventory);?></b></td>
<td>Description: <b><?=$inventory[key($inventory)]?></b></td>
<td>Qty: <?=$_SESSION['items'][$i]?></td></tr>
<?
next($inventory);
$total = $total + $_SESSION['items'][$i];
}
?>
<tr><td colspan=3 align=right>Total: <?=$total?></td></tr>
</table>
<p>A confirmation order will be sent to the email address: <b><?=$_SESSION['email']?></b>, after you have finished your order.
<p>
<form action=session_cart.php>
<input type="submit" name="continue" value="add more items">
</form>
<form action=session_cart_checkout.php>
<input type="submit" name="finish" value="finish order">
</form>
<?
}
?>
I put the name and email set to 0 since that was the only way i could think of to make them $_SESSION variables. But it does not update then with the form, so on the checkout it lists the user as 0. IT bascally does not work at all. The items are updated on the first page, but once i click, checkout, the amoutn of items are always 0.
I have spent all day trying to translate the examples in this book from older php to the new style, and it is not working that well :/ I would appreciate any and all help. And yes, i have had the php manual .chm open all day as well, and looked through several other books, but i cannot seem to narrow down my problem to anything i could search for. I need some help from someone who can look at it and tell me where i messed up.
Thx in advance.