
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 );