Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript AJAX Basics (retiring) jQuery and AJAX Posting Data with jQuery

Jose Morales-Mendizabal
Jose Morales-Mendizabal
19,175 Points

AJAX or PHP solution for posting data and staying on same page?

in AJAX basics, Dave McFarland shows how to post data from a form, and load in new HTML markup upon success, all without ever leaving the page.

Now, in "Building a simple PHP application" course (specifically in the last 3 videos of the Adding a contact form section), Andy Hoyt shows a pure PHP method to do the same. This server side method uses conditional statements to display markup depending on the $_SERVER variable status.

I want to implement this feature in a website I'm building. Which solution should I use? is there an advantage on using one over the other?


1 Answer

Stone Preston
Stone Preston
42,016 Points

I think you may have to use AJAX if you dont want to reload the page (I may be wrong). The PHP method you mention might not navigate away from the contact page, but when the form is submitted I think it has to reload the page.

In other words I think with AJAX you send a request to the server, it sends a response back and changes can be seen on the page, no refresh or reload of any kind takes place. With PHP you send a request to the server, it executes the conditional PHP code, then sends the response back and the page has to be reloaded for changes to be seen. So if you want your dynamic changes to be seamless and not require any reloading then you will need to use AJAX.