简体   繁体   中英

redirect function doesnt work properly after submitting the form twice

I did a form validation where server checks submitted data. If data passes validation, then redirect to previous page, if not, a modal box will pop up tells user wrong username/password.

The question is , after user resubmit the form, redierct function doesn't work. It only works when user first time successfully input those fields. Can anyone help me with this please?

The html code:

<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>

The php code:

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();
        }
    }
}
?>

Use javascript to redirect.

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

Apart from using header() to redirect, you can use meta refresh method, or JS window.location method.

  <script>
     window.location = "<?=$_POST['referer']?>"
  </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