Ajax is getting popular these days. Many would wanna convert their traditional shopping carts for their online stores into Ajax-driven, which needent page refresh. A tutorial example has been posted on PHPBuilder how to use Prototype on the JavaScript client side, to make ajax remote calls and PHP on the server to store the cart items.
On the client side, a few functions need to be done as follows using Prototype. Of course, be sure that you include the prototype.js in between your <head></head> tags
The main function of JavaScript that uses Prototype to send the remote ajax call (sending the task and the item no parameters to the managecart.php via AJAX) is the manageCart, with an additional function to report error as follows:
function manageCart(task, item)
{
var url = ‘managecart.php’;
var params = ‘task=’ + task + ‘&item=’ + item;
var ajax = new Ajax.Updater(
{success: ‘cartResult’},
url,
{method: ‘get’, parameters: params, onFailure: reportError});
}
</script>
function reportError(request) {
$F(’cartResult’) = “An error occurred”;
}
On the page where the shopping cart is being displayed is a DIV that has the id “cartResult” as follows:
<p>
iPod Leather Case<br />
<a href=”#” onclick=”manageCart(’add’,1)”;>Add this item to Cart</a>
</p>
<p>
Brando USB 4-port hub
<a href=”#” onclick=”manageCart(’add’,3)”;>Add this item to Cart</a>
</p>
<p>
<a href=”viewcart.php”>View your shopping cart</a>
</p&&t;
And on the server side, the managecart.php which does simple task, and being passed with two parameters, which the first one is the task which determines whether it shall add a new item to the cart or delete an item. And the second parameter is the item no that uniquely identifies each item that is stored in the database. And the items will be stored on the instance of cart.class.php, which items are stored in array and then being stored in PHP session.
include(”cart.class.php”);
session_start();
$items = $_SESSION["cart"];
$cart = new Shopping_Cart($items);
// Retrieve the parameters
$task = $_GET['task'];
$item = $_GET['item'];
if ($task == “add”) {
$cart->addToCart($item);
$_SESSION["cart"] = $cart->getCart();
echo “item added!”;
} else {
$cart->deleteFromCart($item);
echo “item deleted!”;
}
?>
And another PHP page, viewcart.php that shows the cart’s content, as follows:
include(”cart.class.php”);
session_start();
?>
<p>
Your Shopping Cart contains:
<?php
print_r($_SESSION["cart"]);
?>
</p>
anybody here know of a good site to find more info on Php _session? I’ve got this site bookmarked and im gonna keep checking it out, but i still would like to find a site that covers Php _session a little more thoroughly..thanks