Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello forum members,


I have a form which has a select menu, allowing users to update the quantities in their shopping cart, similar to the E-Commerce example in chapter 19. Now I'd like to add Ajax functionality to the page.

I have tried the $.ajax() and .load() methods but haven't had any luck so far. Surely I can use these Ajax methods to post data to the same page, so that a page load isn't necessary?

Any ideas will be more than welcome. Thank you.

Link to comment
Share on other sites

Hi HartleySan,


thanks for replying.



Set up an onchange event listener for the select element, and then call the ajax method within the listener.

Beyond that, it's simply a matter of adding the correction logic to the PHP script called, etc.



Thank you, I'll give that a try.



And by the way, didn't we talk about not sending Ajax requests to the same PHP file before?


Yes, but why is a request to the same page a bad thing? Should I always send an Ajax request to another PHP file? I'm wondering what to do about


  1. sessions
  2. times when a user has JavaScript disabled. Must I keep the script, for example, add_cart.php in my existing file and place a copy of it in an external file for the Ajax request? How can I ensure that both scripts are the same should I make a change in one? Different variable or database column names etc..
  3. The external PHP file might not have access to all of the variables that have been defined in the existing file. Must I send all of this info with the Ajax request?


Sorry for all of these questions.


Thanks again.

Link to comment
Share on other sites

It's neither an always or a never thing. It's a what-makes-the-most-sense thing.


Just like you'd never have one PHP script that managed an entire site, it very rarely makes sense to have one script that both handles the initial load and handles all the Ajax requests. Generally, they're just too fundamentally different things for one script to efficiently handle.

If you don't believe me, try to do everything in one script, and I think you'll quickly see what I'm saying.


As for sessions, there is no concern there. All scripts have access to the same user session.


As for JS being disabled, that will not affect which script an Ajax request is sent to because you can't use Ajax at all when JS is disabled. If you want to have a site that works with and without JS, then you should first code the whole site without any JS, and then go back and add the JS layer on top of that. Of course, that adds a lot of time to development, and honestly, these days, the gains of doing so are much more minimal than they were in the past.


Lastly, when you make an Ajax request, you need to send whatever info is required, but that's no different than any non-Ajax request. The web is stateless (minus cookies and sessions), so nothing is remembered between script calls, and the data that is required must be passed back and forth accordingly.

  • Upvote 1
Link to comment
Share on other sites

Thanks HartleySan,


I really appreciate you answering all of my questions. It's nice to get help from someone with your level of expertise. My "coding skills" are a lot better than what they were a year ago (at least I hope so), but there's still so much more to learn. I sometimes find it difficult to keep track of all of these functions and methods and classes etc etc..


Anyway, let me get on with it. Thanks again for helping me :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...