Submit the form using jQuery

Keywords: JQuery JSON Javascript IE

I want to use jQuery Submit the form.Can anyone provide code, demo or example links?

#1st floor

From the manual: jQuery Doc

$("form:first").submit();

#2nd floor

You will have to use $("#formId").submit().

Typically, you can call this function from a function.

For example:

<input type='button' value='Submit form' onClick='submitDetailsForm()' />

<script language="javascript" type="text/javascript">
    function submitDetailsForm() {
       $("#formId").submit();
    }
</script>

You can do this at Jquery website Get more information on.

#3rd floor

$("form:first").submit();

see also Event/Submission .

#4th floor

You can also submit using ajax using the jquery form plug-in:

http://malsup.com/jquery/form/

#5th floor

This depends on whether you submit the form normally or through an AJAX call.You can do this at On jquery.com A lot of information was found, including documentation with examples.To submit the form normally, check it out at this site submit() Method.about AJAX , there are many different possibilities, although you may want to use ajax() or post() Method.Note that post() is actually just a convenient way to call the ajax() method using a simplified and restricted interface.

An important resource I use every day is jQuery How Works Should be used as a bookmark.It has tutorials for using jQuery and the left navigation provides access to all documents.

Example:

normal

$('form#myForm').submit();

AJAX

$('input#submitButton').click( function() {
    $.post( 'some-url', $('form#myForm').serialize(), function(data) {
         // ... do something with response from server
       },
       'json' // I expect a JSON response
    );
});

$('input#submitButton').click( function() {
    $.ajax({
        url: 'some-url',
        type: 'post',
        dataType: 'json',
        data: $('form#myForm').serialize(),
        success: function(data) {
                   // ... do something with the data...
                 }
    });
});

Note that the ajax() and post() methods above are equivalent.You can add other parameters to the ajax() request to handle errors, and so on.

#6th floor

In jQuery, I want the following:

$("#form-id").submit()

But again, you really don't need jQuery to perform this task - just use regular JavaScript:

document.getElementById("form-id").submit()

#7th floor

I also submitted a form through Ajax (not actually submitted) using the following:

  jQuery.get("process_form.php"+$("#form_id").serialize(), {}, 
    function() {
      alert("Okay!"); 
    });

#8th floor

Note that there are problems with dynamically created forms in Internet Explorer.This creates a form that will not be submitted in IE (9):

var form = $('<form method="post" action="/test/Delete/">' +
             '<input type="hidden" name="id" value="' + myid + '"></form>');
$(form).submit();

To make it work in IE, create a form element and attach it before submitting it, as follows:

var form = document.createElement("form");
$(form).attr("action", "/test/Delete")
       .attr("method", "post");
$(form).html('<input type="hidden" name="id" value="' + myid + '" />');
document.body.appendChild(form);
$(form).submit();
document.body.removeChild(form);

Creating a form like Example 1 and attaching it will not work - in IE9, it will cause a JScript error DOM Exception: HIERARCHY_REQUEST_ERR (3)

Props for Tommy W@ https://stackoverflow.com/a/6694054/694325

#9th floor

This sends a table with a preloader:

var a=$('#yourform').serialize();
$.ajax({
    type:'post',
    url:'receiver url',
    data:a,
    beforeSend:function(){
        launchpreloader();
    },
    complete:function(){
        stopPreloader();
    },
    success:function(result){
         alert(result);
    }
});

I have some tips for randomly rebuilding tabular data publishing http://www.jackart4.com/article.html

#10th floor

Techniques for IE dynamic tables:

$('#someform').find('input,select,textarea').serialize();

#11th floor

So far, the solution requires that you know the ID of the form.

Use this code to submit the form without knowing the ID:

function handleForm(field) {
    $(field).closest("form").submit();
}

For example, if you want to handle the click event for a button, you can use

$("#buttonID").click(function() {
    handleForm(this);    
});

#12th floor

You can use it this way:

  $('#formId').submit();

Or

document.formName.submit();

13th floor

When you have an existing form, you should be able to use jquery-ajax / post now:

  • Persist in Submitting - Your Form Events
  • Default function to block submissions
  • Be your own thing

    $(function() { //hang on event of form with id=myform $("#myform").submit(function(e) { //prevent Default functionality e.preventDefault(); //get the action-url of the form var actionurl = e.currentTarget.action; //do your own request an handle the results $.ajax({ url: actionurl, type: 'post', dataType: 'application/json', data: $("#myform").serialize(), success: function(data) { ... do something with the data... } }); }); });

Note that in order for the serialize() function to work in the example above, all form elements need to have their name property defined.

Table example:

<form id="myform" method="post" action="http://example.com/do_recieve_request">

<input type="text" size="20" value="default value" name="my_input_field">
..
.
</form>

@PtF - In this example, the data is submitted using POST, so you can access the data as follows

 $_POST['dataproperty1'] 

Where dataproperty1 is the "variable name" in json.

If you use CodeIgniter, here is a syntax example:

 $pdata = $this->input->post();
 $prop1 = $pdata['prop1'];
 $prop1 = $pdata['prop2'];

#14th floor

Use it to submit a form using jquery.This is a link http://api.jquery.com/submit/

<form id="form" method="post" action="#">
    <input type="text" id="input">
    <input type="button" id="button" value="Submit">
</form>

<script type="text/javascript">
$(document).ready(function () {
    $( "#button" ).click(function() {
        $( "#form" ).submit();
    });
});
</script>

#15th floor

Note that if you already have a Submit event listener installed for the form, commit() is called internally

jQuery('#<form-id>').submit( function(e){ 
    e.preventDefault();
    // maybe some validation in here
    if ( <form-is-valid> ) jQuery('#<form-id>').submit();
});

The attempt to install a new event listener for this form's Submit event (failed) will not work.Therefore, you must access the HTML element itself (unpacked from jQquery) and call commit () directly on it:

    jQuery('#<form-id>').submit( function(e){ 
      e.preventDefault();
      // note the [0] array access:
      if ( <form-is-valid> ) jQuery('#<form-id>')[0].submit();
    });

16th floor

If the button is between the form tags, I prefer the following versions:

$('.my-button').click(function (event) {
    var $target = $( event.target );
    $target.closest("form").submit();
});

#17 Floor

My method is slightly different Change the button to the Submit button and click

$("#submit").click(function(event) {
$(this).attr('type','submit');
$(this).click();
});

#18th floor

relevant information
If someone else uses it

$('#formId').submit();

Don't do that

<button name = "submit">

It took several hours to find that my commit() could not work like this.

#19th floor

I recommend a generic solution, so you don't have to add code for each form.Use the jQuery form plug-in (http://jquery.malsup.com/form/) and add this code.

$(function(){
$('form.ajax_submit').submit(function() {
    $(this).ajaxSubmit();
            //validation and other stuff
        return false; 
});

});

6520th floor

$( 'form' ).on( 'submit' , () => { // arrow function console.log( $( this ) ); // form } );
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.js"></script> <form> <input type="text" value="Hello there"> <input type="submit" value="Go"> </form> 

#21th floor

function  form_submit(form_id,filename){
    $.post(filename,$("#"+form_id).serialize(), function(data){
        alert(data);
    });
}

It will publish the form data to the given file name through AJAX.

_#22nd floor

jQuery("a[id=atag]").click( function(){

    jQuery('#form-id').submit();      

            **OR**

    jQuery(this).parents("#form-id").submit();
});

_Floor

You can do this:

$('#myform').bind('submit', function(){ ... });
Published 0 original articles, won approval 2, visited 5586
Private letter follow

Posted by Pooptart on Wed, 22 Jan 2020 20:38:50 -0800