简体   繁体   中英

Auto submit login form and start session

I am trying to auto submit login form by using javascript but it cannot start session and redirect to same page but when i am try to submit same form without javascript it working. I need to submit form automatically. Any suggestion?

<?php include('_header.php'); ?>
    <form id="my_form" method="post" action="index.php" name="loginform">
        <label for="user_name"><?php echo WORDING_USERNAME; ?></label>
        <input id="user_name" type="text" value="something" name="user_name" required />
        <label for="user_password"><?php echo WORDING_PASSWORD; ?></label>
        <input id="user_password" type="hidden" value="something1" name="user_password" />
        <input type="checkbox" id="user_rememberme" name="user_rememberme" value="1" />
        <label for="user_rememberme"><?php echo WORDING_REMEMBER_ME; ?></label>
        <input type="submit" name="login" value="<?php echo WORDING_LOGIN; ?>" />
    </form>
    <script type="text/javascript">
        function submitForm() {
            document.getElementById('my_form').submit();
        }
        window.onload = submitForm;
    </script>
    <a href="register.php"><?php echo WORDING_REGISTER_NEW_ACCOUNT; ?></a>
    <a href="password_reset.php"><?php echo WORDING_FORGOT_MY_PASSWORD; ?></a>
<?php include('_footer.php'); ?>

If you want to autosubmit your form upon page visit you simply put this code after your form:

<script>
     document.getElementById('my_form').submit();
</script>

the problem is that you are submiting form over and over again, try this, check if form has already been submited and if not submit a form otherwise skip submition

<script type="text/javascript">
        function submitForm() {
            document.getElementById('my_form').submit();
        }
 <?php if(!isset($_REQUEST['login']) ) : ?>  window.onload = submitForm; <?php endif ?>

</script>

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