Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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?

Thanks!

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.