提交表单两次后,重定向功能无法正常工作

时间:2017-12-07 04:24:53

标签: javascript php html

我做了一个表单验证,其中服务器检查提交的数据。如果数据通过验证,然后重定向到上一页,如果没有,将弹出一个模态框告诉用户错误的用户名/密码。

问题是,在用户重新提交表单后,redierct函数不起作用。它仅在用户第一次成功输入这些字段时有效。有人可以帮我这个吗?

HTML代码:

<div id="formInfo">
    <form method="post" action="<?PHP echo $_SERVER['PHP_SELF'];?>">
        <span class="title">Sign in to urmajesty.net</span><br><br>

        <div id="input_container">
            <input type="text" name="username" placeholder="username"><img src="Images/icons/user.png" id="input_img"><br><br>
        </div>

        <div id="psw_container">
             <input type="password" name="psw" placeholder="password"><img src="Images/icons/key.png" id="key_img"><br><br>
        </div>

        <div id="checkbox">
            <input type="checkbox" name="RememberUsername" value="Yes"><span class="checkboxtxt">Remember my username</span>
        </div><br><br>

        <div id="Forget">
            <a href="#"><span class="forget1">Forget Password</span></a><a href="#"><span class="forget2">Forget Username</span></a>
        </div>

        <input type="submit" class="button" value="SIGN IN"><br><br>

        <div id="hispan"><p class="hip"><span class="hi">New to urmajesty.net?</span></p></div><br>

             <input class='disable_me' name="referer" type="hidden" value="<?php echo urlencode($_SERVER['HTTP_REFERER'])?> " />
             <button type="button" class="button" id="btn">CREATE ACCOUNT</button>
    </form>
</div>

<div id='modal_box'>
    <div class='modal_content'>
         <span class='close'>&times;</span><p>username/password is wrong. Please try again!</p>
    </div>
</div>

php代码:

if($_SERVER["REQUEST_METHOD"]=="POST"){
    $validation=Login();
    if($validation==false){
        echo"<script>
        var modal_box=document.getElementById('modal_box');
        modal_box.style.display='block';    
        modal_box.style.backgroundColor='rgba(0,0,0,0.4)';
        var close=document.getElementsByClassName('close')[0];
        close.onclick=function(){
            modal_box.style.display='none';
        }
        window.onclick=function(event){
            if(event.target==modal_box){
                modal_box.style.display='none';
            }
        }
        </script>";
    }
    else{
        if(!empty($_POST['referer']) && !is_array($_POST['referer'])){
            header("Location: ".urldecode($_POST['referer']));
            exit();
        }
    }
}
?>

2 个答案:

答案 0 :(得分:0)

使用javascript重定向。

if(!empty($_POST['referer']) && !is_array($_POST['referer'])){ ?>
    //header("Location: ".urldecode($_POST['referer']));
    <script>
        window.location.href = "<?=$_POST['referer']?>";
    </script>
 <?php
    exit();
 } ?>

答案 1 :(得分:0)

除了使用header()重定向之外,您还可以使用元刷新方法或JS window.location方法。

  <script>
     window.location = "<?=$_POST['referer']?>"
  </script>