How do I create an Ajax Listener, so content changes dynamic

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I create an Ajax Listener, so content changes dyn

Post by Celauran »

simonmlewis wrote:I take it the "URL" part is the path to the PHP file that runs all the database query code?
Yes
simonmlewis wrote:What is the "item_id: item, quantity: qty"?
The item and quantity of said item being added to the cart. Just a pseudo-code example, though. These would be the inputs being passed to your PHP functions.
simonmlewis wrote:How is that taking information from the query, as I think I see that then going into the next success: part of the function.
You're sending a request to a URL. Whatever response is returned from that request is what gets passed to the success callback. In the discussion above, you were returning the new number of items in the cart, which is what I mocked up. In practice, it could be anything.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I create an Ajax Listener, so content changes dyn

Post by Christopher »

One concept you may be missing is using JSON. I have PHP convert all the data I send to Javascript to JSON. It can then be either embedded into the page when the page is generated, or sent back from an Ajax call. Then within Javascript you are just dealing with an object or array because JSON is essentially code. From that point it should be easy for you to update the cart display.
(#10850)
Post Reply