Click to See Complete Forum and Search --> : session/cookies


jrthor2
11-05-2007, 07:44 PM
I have a site where I am going to have a button under each product for "Add to Order Form". What I want to do, is somehow when the user clicks this button, store the data either in session or a cookie (whichever would be best), and then when they go to the order form, it would display the items, price, description, have a box for them to fill in the quantity, and then have a total column. How would I go about storing the data and then selecting the product info from my mysql database?

Thanks!!

NogDog
11-05-2007, 09:49 PM
One method would be to use an array in the $_SESSION array, e.g.:

<?php
session_start();

// ...form validation code, etc....

$_SESSION['cart'][] = array(
'item_id' => $_POST['item_id'],
'item_qty' => $_POST['item_qty']
);

Then when you want to process the items in the cart, you just loop through it:

<?php
session_start();

// . . .
?>
<table>
<?php
foreach($_SESSION['cart'] as $item)
{
echo "<tr><td>" . $item['item_id'] . "</td><td>" > $item['item_qty'] . "</td></tr>\n";
}
?>
</table>

jrthor2
11-06-2007, 07:10 AM
What would the code be for my button? Would I just have a form tag around my entire page with a method of post?

jrthor2
11-06-2007, 09:18 AM
I tried this as my code for my page, but when I click the "Add to order form" button on any item, it adds the detail for the last item displayed, not the item I clicked.

<?
include $_SERVER['DOCUMENT_ROOT'] . '/inc/session.php';
if ($_POST) {
$_SESSION['orderForm'][] = array(
'item_id' => $_POST['item_id'],
'item_name' => $_POST['item_name'],
'item_price' => $_POST['item_price'],
'item_min_qty' => $_POST['item_min_qty']
);
print_r($_SESSION['orderForm']);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?
$me=ereg("/", __FILE__) ? substr(strrchr(__FILE__,"/"),1) : __FILE__;

$$me=1;

$section="product_search";

$navfilename="/nav/toolbar.php";
$rightColName="/inc/right_col_home.php";

include $_SERVER['DOCUMENT_ROOT'] . '/inc/sitecommon.php';
include $_SERVER['DOCUMENT_ROOT'] . '/inc/config.php';
include $_SERVER['DOCUMENT_ROOT'] . '/inc/opendb.php';


# use a get_string'd today if we're passed it

if (!strlen(trim($today))){
$today=strftime("%Y-%m-%d");
}
?>
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<? include $_SERVER['DOCUMENT_ROOT'] . '/inc/head_include.php';?>
<title><?=$sitename?> : Product Listing</title>
</head>
<body>
<div id="wrap">
<? include $_SERVER['DOCUMENT_ROOT'] . '/inc/header.php';?>
<? include $_SERVER['DOCUMENT_ROOT'] . $navfilename; ?>
<div id="centerCol">
<div id="leftFrontPg">
<form id="additems" name="additems" method="post" action="select_medical.php">
<?
$sql = "SELECT id, name, description, price, min_qty, image from products where category=1";
$result = mysql_query($sql);
// Check result
// This shows the actual query sent to MySQL, and the error. Useful for debugging.
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $sql;
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
$min_qty = $row['min_qty'];
$image = $row['image'];
?>
<div class="listings">
<div class="column1">
<img src="<?=$image;?>" width="150px" height="150px" alt="<?=$name;?>" />
<br />
<h4><?=$name;?></h4>
<p>
<?=$description;?>
<br />
Min. Qty: <?=$min_qty;?>
<br />
<strong>Price:</strong> $<?=$price;?> ea.
</p>
<input type="hidden" name="item_id" id="item_id" value="<?=$id;?>" />
<input type="hidden" name="item_name" id="item_name" value="<?=$name;?>" />
<input type="hidden" name="item_price" id="item_price" value="<?=$price;?>" />
<input type="hidden" name="item_min_qty" id="item_min_qty" value="<?=$min_qty;?>" />
<input type="submit" id="add" value="Add To Order Form" />
</div>
</div>
<?
}
// Free the resources associated with the result set
// This is done automatically at the end of the script
mysql_free_result($result);
?>
</form>
</div>
<? include $_SERVER['DOCUMENT_ROOT'] . $rightColName;?>
</div>
<? include $_SERVER['DOCUMENT_ROOT'] . '/inc/footer.php';?>
</div>
<? include $_SERVER['DOCUMENT_ROOT'] . '/inc/closedb.php';?>
</body>
</html>

Also, when I click the button for the items, it reloads the page. Is there a way to add the item detail to the session, but not reload the page?

Thanks for all the help!!

bokeh
11-06-2007, 09:53 AM
Use Javascript to store the data in a cookie. That way it will be sent to the server on the next page load. Test that the cookie is set, (with Javascript) then return false on the button so the page doesn't refresh.

jrthor2
11-06-2007, 10:14 AM
any code help would be greatly appreciated

bokeh
11-06-2007, 11:11 AM
any code help would be greatly appreciated// javascript
function WriteCookie(name, value, seconds)
{
expireDate = new Date;
expireDate.setSeconds(expireDate.getSeconds() + seconds);
document.cookie = name + '=' + value + ';expires=' + expireDate.toGMTString();
return (ReadCookie(name)?true:false)
}

function ReadCookie(name)
{
var cookies = document.cookie;
if (cookies.indexOf(name) != -1)
{
var startpos = cookies.indexOf(name)+name.length+1;
var endpos = cookies.indexOf(";",startpos)-1;
if (endpos == -2) endpos = cookies.length;
return unescape(cookies.substring(startpos,endpos));
}
else
{
return false;
}
}

jrthor2
11-06-2007, 12:12 PM
I found the below on another page, that works there, but I can't seem to get it working. It uses a checkbox to set/unset the cookie values. Could someone help? I always get to the alert('item unchecked!2'); but nothing after that

checkbox code is:

<input type="checkbox" name="item_id<?=$id;?>" value="<?=$id;?>~<?=$id;?>" onclick="javascript: SetCheckboxes(this.form, this.name);" <? if (strstr($ggp_orderlist,"item_id:".$id)) {?> checked="checked"<? } ?> />

The javascript code is attached.

Thanks!

jrthor2
11-06-2007, 12:22 PM
I got that problem fixed, I removed the_ in my checkbox name value and it set my cookie.