Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I am guessing a little here. By not preventingdefault, the form will POST the data to the server. If you switch to AJAX you have to provide the body as per documentation. However, I do not rememb...
Answer
#2: Post edited
- I am guessing a little here. By not preventingdefault, the form will POST the data to the server.
- If you switch to AJAX you have to provide the body as [per documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send).
- However, I do not remember to ever using the XMLHttpRequest directly, because there are wrappers to help you. One example using a more modern approach ([fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)) is provided in [this answer](https://stackoverflow.com/a/45529553/2780791):
- const url = "http://example.com";
- fetch(url, {
- method : "POST",
- body: new FormData(document.getElementById("contact_form")),
- }).then(
- response => response.text() // .json(), etc.
- ).then(
- html => console.log(html)
);
- I am guessing a little here. By not preventingdefault, the form will POST the data to the server.
- If you switch to AJAX you have to provide the body as [per documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send).
- However, I do not remember to ever using the XMLHttpRequest directly, because there are wrappers to help you. One example using a more modern approach ([fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)) is provided in [this answer](https://stackoverflow.com/a/45529553/2780791):
- const url = "http://example.com";
- fetch(url, {
- method : "POST",
- body: new FormData(document.getElementById("contact_form")),
- }).then(
- response => response.text() // .json(), etc.
- ).then(
- html => console.log(html)
- );
- A jquery way of achieving a similar HTTP call is provided by [this answer](https://stackoverflow.com/a/5772905/2780791):
- $(document).ready(function(){
- $("#frm").submit( function () {
- $.post(
- 'script.php',
- $(this).serialize(),
- function(data){
- // do something with data
- }
- );
- return false;
- });
- });
#1: Initial revision
I am guessing a little here. By not preventingdefault, the form will POST the data to the server. If you switch to AJAX you have to provide the body as [per documentation](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send). However, I do not remember to ever using the XMLHttpRequest directly, because there are wrappers to help you. One example using a more modern approach ([fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch)) is provided in [this answer](https://stackoverflow.com/a/45529553/2780791): const url = "http://example.com"; fetch(url, { method : "POST", body: new FormData(document.getElementById("contact_form")), }).then( response => response.text() // .json(), etc. ).then( html => console.log(html) );