简体   繁体   中英

Submit form on page load

I want this login form to submit automatically when the page loads. I am using IP address for registration so do not want a login button.

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
     <input type="hidden" name="ip_address" /><br />

    <input type="submit" name="submit" value="Login" />
</form>

You can do something like:

<form id="my_form" action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
     <input id="ip_address" type="hidden" name="ip_address" /><br />

    <input type="submit" name="submit" value="Login" />
</form>
<script>
    $(document).ready(function(){
        if($("#ip_address").val() != "")
        {
            $("#my_form").submit();
        }
    });
</script>

Note: you must add JQuery library in your web page.

Using JavaScript:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" id="my-form">
    <input type="hidden" name="ip_address" /><br />
    <input type="submit" name="submit" value="Login" />
</form>
<script>
    //Once the page is loaded, submit the form
    document.addEventListener("DOMContentLoaded", function(event) {
        //The id of your form.
        var idOfYourForm = "my-form";
        document.getElementById(idOfYourForm).submit();
    });
</script>

You just need to set the id attribute to your form and set the variable idOfYourForm , so the JavaScript will be able to submit your form. This method use pure JavaScript, no jQuery or other library.

See DOMContentLoaded event.

In case you don't use jQuery:

<body onload="javascript:submitonload();">
<form id="my_form" action="<?=$_SERVER['PHP_SELF'];?>" method="post">

<input type="hidden" name="ip_address" value="<?=(isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '');?>" />
<br />
<input type="submit" name="submit" value="Login" />
</form>
<script type="text/javascript">
    function submitonload() {
document.getElementById('my_form').submit();
}
</script>
</body>

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