简体   繁体   中英

How To Submit A Form On WebPage Load Automatically?

I have a form in my web page and I want to submit form data automatically when user open this on his web browser. This form contain fixed data from my side and use didnt have to add any data. So here is my form code...

<form action="data.php" method="post" enctype="plain" id="theForm">   
<input type="text" name="Visitor" value="FIXED-DATA-FROm-ME" />
<input type="submit" value="Send" />
</form>

How to do this? Please share some simple steps or light coding...

Yes, You can do this using JavaScript too. Just add the below JavaScript code on your form page and its done. When any of your visitor open this page then all the form data will be submit automatically...

<script type="text/javascript">
window.onload = function() {
    var form = document.getElementById("theForm");
    form.submit();
}
</script>

If you dont want the user to be redirected, but you want to do it in the background, you should use ajax. Like:

$.ajax({
        url: 'data.php',
        type: 'post',
        dataType: 'json',
        data: $('form#theForm').serialize(),
        success: function(data) {
                   ...  success ...
                 }
});

you can try this

Jquery :

$(document).ready(function(){
  $('#theForm').submit();
});

Javascript:

window.onload = function() {
   document.getElementById("theForm").submit();
}

If all the data are static,You dont need the form at all. call an ajax page on page load (document.ready()) and in the ajax page you just insert your data to the database table

Do any of the following:

  • Using javascript onload method

    window.onload=function(){ var form = document.getElementById("theForm"); form.submit(); };

  • Using onload with body tag

    Make a function in javascript

    function myFunction() { var form = document.getElementById("theForm"); form.submit(); }

    Add following to the body tag in html

    <body onload="myFunction()">

  • Using jquery .ready() function See here

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