简体   繁体   中英

Submit a form with ajax on page load

I have this form that I would like to be submitted on page load.

My jQuery code is currently as follows:

<script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
      document.getElementById('form1').submit();
    }); 
</script> 

I have tried the ajax form plugin ( http://malsup.com/jquery/form/ ) which does submit the form but I have no idea how to get it working with on page load:

  <script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
        $('#form1').ajaxForm(function() { 
            alert("Thank You"); 
        }); 
    }); 
</script> 

Help!

I just want form1 submitted once the page loads, the plugin looks like my best bet however I don't know how to get it working on page load

Try this

$(document).ready(function() { 
    $('#btnSubmit').click(); //assuming this is your submit button
}); 

I hope it help you.

I hope this will do what you want :)

jQuery(document).ready(function(){

  // #1 get the data to submit
  var jInput;
  var oData  = {};
  var aInput = jQuery('#form1 input');
  var i      = aInput.length;
  while ( --i !== -1 ) {
    jInput = jQuery( aInput[i] );
    oData[ jInput.attr('id') ] = jInput.val();
    console.log( jInput.val() );
  }

  // #2 submit data via ajax (method == 'post')
  // without response
  jQuery.post( 'url-to-send-ajax-request-to.php', oData );

  // with response
  jQuery.post( 'url-to-send-ajax-request-to.php', oData, function( sResponse ){
    alert( sResponse ); // would be the output of the file you sent your ajax request to
    alert('Thank You');
  });

});

PHP-file ('url-to-send-ajax-request-to.php'):

print_r( $_POST );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM