Shopping cart in jquery with php and session

marikudo picture marikudo · Oct 8, 2010 · Viewed 7.5k times · Source

Good day!

I'm now learning this jquery by creating a basic shopping cart were as add to cart buttons, remove items and clear buttons works in jquery.

The function is to save the added items on session and can remove and clear also by jquery. I have an idea now on how the jquery works on adding the items. But not on removing a single item or to clear all of them on the cart.

Any help, suggestion are much appreciated.

thanks... :D

Here is the code I have done so far on shopping cart. This is in two files the index.php and cart.php were the add to cart button already working except for remove and clear. Because I was not able to run the session yet.

Here it is:

<script type="text/javascript">
// for ajax 
$(document).ready(function(){

  $(".add").click(function(){

    var id = $(this).attr("proid");
    var item = $("#item"+id).val();
    var brand = $("#brand"+id).val();   
    var price = $("#price"+id).val();   
    var quantity = $("#quantity"+id).val();         

    $.ajax({ 
      type: "POST",
      url: "cart.php",
      data: "id="+ id + "& item="+ item + "& brand="+ brand +"& price="+ price +"& quantity="+ quantity,
      success: function(data){
        $("#jcart").html(data);
      }
    }); //ajax

    return false;

  }); //click function

  //show empty table
  $("#jcart").load("jcart.php");

  //--> for options
  $(".remove").click(function(){

    var id = $(this).attr("proid");

    $.ajax({
      type: "POST",
      url: "clear.php",
      data: "id="+ id,
      success: function(data){
        $('#jcart').html(data)
      }
    });

  });       

  $(".clear").click(function(){

    $.ajax({
      type: "POST",
      url: "clear.php",
      success: function(data){
        $("#jcart").html(data)
      }
    });

  });

});

</script>

I'm making difficult with the session on how the items being save then retrieve. Any ideas or help will much appreciated...

thanks in advance!

Answer

Luke Stevenson picture Luke Stevenson · Oct 8, 2010

There are a fair few tutorials out there which create (what sounds like) what you are looking for.

Have a read of them, along with the PHP Manual for Sessions, and see what you can come up with. If you make a start, and then get stuck, come back, ask some more questions (with examples and code samples) and we will do what we can to help.