To submit a form using jQuery, you can use the .submit()
method. Here’s an example of how you can do it:
HTML:
<form id="myForm" action="submit.php" method="POST"> <input type="text" name="name" /> <input type="email" name="email" /> <input type="submit" value="Submit" /> </form>
Submit form in jquery
JavaScript (with jQuery):
$(document).ready(function() { // Capture the form submit event $('#myForm').submit(function(event) { event.preventDefault(); // Prevent the default form submission // Perform any form validation or processing here // Make an AJAX request to submit the form data $.ajax({ url: $(this).attr('action'), method: $(this).attr('method'), data: $(this).serialize(), success: function(response) { // Handle the success response console.log(response); }, error: function(xhr, status, error) { // Handle the error response console.log(error); } }); }); });
In this example, the form has an id
attribute of “myForm”. The JavaScript code captures the form’s submit event using the $('#myForm').submit()
function. It then prevents the default form submission using event.preventDefault()
.
Inside the submit event handler, you can perform any form validation or data processing that you need. Then, an AJAX request is made using $.ajax()
. The URL and method of the request are obtained from the form’s attributes using .attr()
. The form data is serialized using $(this).serialize()
.
You can handle the success and error responses within the corresponding callback functions of the AJAX request (success
and error
).
Make sure to replace “submit.php” with the actual URL where you want to submit the form data.
This example demonstrates a basic implementation of form submission using jQuery. You can customize it further based on your specific requirements.
If this not work:
Just copy and past this code this works for me to submit form in jquery.
<script> $(document).ready(function (abc) { $("#catnm").on('submit', (function (abc) { abc.preventDefault(); $.ajax({ url: "your_form_submit_url.php", type: "POST", data: new FormData(this), contentType: false, cache: false, processData: false, success: function (data) { alert(data); }, error: function () { alert('error'); } }) })) }); </script>
To no more about submitting the form in jquery click here. And read more about how jquery works.