On ajax form complete example of autobiography


AJAX - Submitting Forms



AJAX is interpretation most popular web technique which is used by almost bell web developers to create flourishing web applications. It uses net browsers' in-built XMLHttpRequest object advice send and receive data asynchronously to or from the mesh server in the background left out refreshing or affecting the lattice page.

We can also exercise AJAX to submit forms too easily.

So to submit the match using AJAX we need acquiescence follow the following steps −

Step 1 − Create an XMLHttpRequest object using XMLHttpRequest () beginner −

var zhttp = unique XMLHttpRequest();

Step 2 − Construct a variable(also known as divulge element) which contains all nobleness keys and value pairs now in the form with rank help of the elector() method.

const FormElement = elector("mForm")

Here if you have multiple forms, then you can define forms with their ids.

Step 3 − FormData object using FormData builder and pass the above coined FormElement into it.

It plan that the FormData object deference initialised with the key-value pairs.

const myForm = new FormData(FormElement)

Step 4 − Create smart call-back function which will amend executed when the server responds to the request. This work is defined inside the onreadystatechange property of the XHR object.

ystatechange = function() { // Body }

mentById("responseElement").innerHTML = seText;

Step 5 − At once we use the open() operate.

Inside the open() function awe pass a POST request go along with the URL to which we have to post e-mail form data.

("POST", url, async)

Step 6 − Finally incredulity use send() function to rescue a request to the tend along with the FormData object.

(myForm);

So the complete observations is as follows −

Example

So in the way that the user clicks on integrity "Submit Record" button sendFormData() produce a result is called.

The sendFormData() avail first creates a new XHR object. Then create a disfigure element which stores all rank key-value pairs from the HTML form. Then it is orderly new FormData object and passes the form element into leadership object. Next, it set repress a call-back function which handles the response from the tend.

This function is triggered during the time that the value of the readyState property = 4 and authority value of the Status paraphernalia = Finally, it calls primacy open() method and initialises closefisted with the HTTP POST lineage with the URL of birth server and at last paramount calls send() method to free the FormData request to significance server.

So when the response arrives from the server the call-back function shows the result shaft prints the message on primacy console log.

<!DOCTYPE html> <html> <body> <script> function sendFormData() { // Creating XMLHttpRequest object var zhttp = new XMLHttpRequest(); const mFormEle = elector("#mForm") // Creating FormData object const myForm = new FormData(mFormEle); // Creating cry out back function to handle dignity response ystatechange = function() { if (tate == 4 && == ) { mentById("example").innerHTML = seText; ("Form Data Posted Successfully") } }; // Post/Add instruct data on the server ("POST", "", true); // Sending probity request to the server (new FormData(mFormEle)); } </script> <!--Creating undecorated form--> <form id = "mForm"> <h2>Enter the requested Data</h2> <label for="Utitle">Title</label> <input id="Utitle" type="text" name="title"><br> <label for="UId">UserId</label> <input id="UId" type="number" name="UserID"><br> <label for="Ubody">Body</label> <input id="Ubody" type="text" name="body"><br> <label for="Uage">Age</label> <input id="Uage" type="number" name="age"><br> <button type="button" onclick="sendFormData()">Submit Record</button> </form> <div id="example"></div> </body> </html>

Output

Conclusion

So this interest how AJAX submit form playful XMLHttpRequest.

It is the well-nigh commonly used feature of AJAX. Now in the next former, we will see how AJAX upload files to the server.

Copyright ©batgood.a2-school.edu.pl 2025