Book Image

PHP Ajax Cookbook

Book Image

PHP Ajax Cookbook

Overview of this book

Table of Contents (16 chapters)
PHP Ajax Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Ajax shopping cart


Shopping carts play an important role in e-commerce websites. In this task, we will learn how to build a shopping cart with Ajax functionality to provide the best user experience. The result of this task will look like the following screenshot:

Getting ready

All we need for this task is the latest jQuery library and a sample .php file, ajax/shopping-cart.php.

This script will provide basic server functionality to retrieve and receive data:

<?php
if($_POST["productID"] && $_POST["action"]){
  $productID = $_POST["productID"];
  $action = $_POST["action"];
  switch($action){
    default:
        $result["status"] = "ERROR";
        $result["message"] = "Product has been added to the shopping cart.";
    break;
    case "add":
        // logic for adding a product to the shopping cart
        $result["status"] = "OK";
        $result["message"] = "Product has been added to the shopping cart.";
    break;
    case "delete":
        // logic for removing a product from...