Ajax for multiple form
Ajax for multiple form
We will use programming in this lesson to attempt to solve the "Ajax for multiple form".
You can use a single ajax function to submit the multiple forms in HTML. You have to use a class for the submit button and the submit button should be inside the HTML form.-
Dynamically get the form action url, method, data and call ajax on click using jQuery
<form action="/post-ajax" method="POST"> <input type="submit" class="submit-form" value="submit" /> </form> <form action="/post-ajax-2" method="POST"> <input type="submit" class="submit-form" value="submit" /> </form> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $('.submit-form').on('click', function(e){ e.preventDefault(); let form = $(this).closest('form'); let formActionUrl = form.attr('action'); let type = form.attr('method'); let formData = form.serialize(); $.ajax({ url: formActionUrl, type: type, data: formData, success: function(response){ console.log(response); }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status); console.log(thrownError); } }); }); }); </script>
This code snippet helps you to get the form attribute values and call ajax dynamically without any changes in javascript code. It will help you to submit multiple forms using the same ajax method.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Jquery
- Preview image in base64 format using jQuery on change input file
- Execute function after Ajax call is complete
- How to preview image before uploading in jQuery
- How to find closest upper element and append html to below it in jquery
- (index):128 Uncaught TypeError: MobileNumberVal.test is not a function
- Show and hide target element on click button using javascript/jquery
- Append method to add text to body not working
- JQuery append html to below the target element
- Uncaught TypeError: $.ajaxSetup is not a function
- Preview image in BLOB URL format on choose file in jQuery
- Mixed Content: The page at 'https://w3codegenerator.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint
- How to use and purpose of localstorage in javascript
- How to check ajax request is completed in jQuery
- How to check object is empty or not in jQuery
- Ajax done fail ajaxcomplete in not a function
- Validate form using jQuery
- How to pass javascript variable to laravel route on ajax call
- How to validate input type file size in jQuery for multiple records
- Add and remove input fields on click button using jQuery
- Get the href link on click anchor in javascript
- How to reset form input element on click
- Uncaught TypeError: $.ajax is not a function
- Jquery.validate.min.js:4 Uncaught TypeError: Cannot read properties of undefined (reading 'mode')
- How to get closest form input value in jQuery
- Show loading icon on click submit button in jQuery