Submit form without page reloading
To submit a form without page reload, use AJAX and the fetch() method:
Swap 'submit.php'
with your server-side script's URL. Data gets sent asynchronously, resulting in no page refresh.
Going incognito with Iframes
If you are dealing with a non-AJAX environment, an iframe serves as your undercover agent:
- Draft up an
iframe
, assign it a unique name, and keep it hidden under the dark shades of display: none. - In your form, set the
target
attribute to the iframe's name. This pits the iframe against the form submission response, winning the fight against page reload. - Set your form's
action
to the PHP element that processes and delivers your form submission to the server. - Maintain the POST method for secure and efficient data submission.
Pagination without AJAX
FormData API pulls off an amazing performance for those whose hearts do not beat for AJAX. It is a slick act that:
- Performs the tricky balancing act of modern browser capabilities and a no AJAX environment.
- Takes the stage with JavaScript and creates a FormData instance from a form element.
Providing feedback: A JavaScript way
Nothing like an appreciative audience. Here's how JavaScript handles the applause:
Combine it with the iframe act for a unifying performance:
Trading off: Know your actors
- The iframe method is a minimalistic script that lacks in direct client-server chat.
- Fetch() with FormData has an impressive portfolio with easy handling of multipart form data.
- Never forget your security guards while scripting server-side (
submit.php
).
Optimizations and quirky tips
Juggling success and error handling
Regardless of your choice of method, form juggling can be a tad tricky. But a graceful error handling can save the day:
Performance and caching
With AJAX, a no-cache magic spell (setting cache:false) maintains a fresh server chat on each form act:
The after-show cleanup routine
For our iframe act, an after-show cleanup leaves the stage (page) sparkly clean:
Was this article helpful?