PHP Classes

Page reload

Recommend this page to a friend!

      Shopping Cart  >  Shopping Cart package blog  >  AJAX Shopping Cart wi...  >  All threads  >  Page reload  >  (Un) Subscribe thread alerts  
Subject:Page reload
Summary:you shouldn't reload the page after each request
Messages:3
Author:Kristof Dekany
Date:2015-09-22 10:27:01
 

  1. Page reload   Reply   Report abuse  
Picture of Kristof Dekany Kristof Dekany - 2015-09-22 10:27:01
If you are using ajax to send data to the server, you really should consider using ajax to update the list too. Otherwise your code doesn't differ from a form with get method much. (Feels kinda pointless)

You should build the html part of the list in the php file, or build a json object, from which you build the html via javascript in the done part of the request.

so instead of location.reload()
you should have somthing like $("table > tbody").html( response.list )
or $("table > tbody").html( buildList(response.listData) )

This way you can send more data with one response, example a variable that show is the request was completed successfully on the server side, or even the length of the cart - so you don't need that extra if.

example php response:
json_encode( [
"ok" => mysql_affected_rows() //or anything you like ex: is the itemId still valid - as pointed out in a previous comment
"data" => $cart->getItems() //sry i haven't read the first part
] );

One final note:
if you chose this methid for updateing the list, then you'll need to delegate the js event handlers to an outer non-chnging element
4ex: $("body").on( "click", "span.removeFromCart", handler );

  2. Re: Page reload   Reply   Report abuse  
Picture of Ashraf Gheith Ashraf Gheith - 2015-09-22 10:35:52 - In reply to message 1 from Kristof Dekany
Thank you for your entry. This was just a simple tutorial to give some basics of how to create a shopping cart.

  3. Re: Page reload   Reply   Report abuse  
Picture of Kristof Dekany Kristof Dekany - 2015-09-22 11:11:52 - In reply to message 2 from Ashraf Gheith
I should have thought that. I didn't wanted to sound like a smartypants.

Nevertheless my comment should give some extra ideas for your audience.