2017-01-11 79 views
-2

我正在製作一個註冊表格,當您成功填寫所有字段時,它會重定向到thank_you_register.php。但我想在幾秒鐘後,thank_you_register.php頁面會自動重定向到另一頁。我該怎麼做呢? 的HTML代碼是這樣的:php自動重定向到頁面幾秒後延遲

<form class="register form" action="../app/controller/authController.php" method="POST"> 
      <div class="form-group"> 
       <select name="region" id=""> 
        <option value="Americas">Americas</option> 
        <option value="Europe">Europe</option> 
        <option value="Asia">Asia</option> 
       </select> 
      </div> 
      <div class="form-group"> 
       <input type="text" name="first_name" id="" placeholder="First Name"> 
       <?php if(isset($_SESSION['first_name_error'])) : ?> 
        <span class="error"><?php echo $_SESSION['first_name_error']; ?></span> 
       <?php unset($_SESSION['first_name_error']); endif; ?> 
      </div> 
      <div class="form-group"> 
       <input type="text" name="last_name" id="" placeholder="Last Name"> 
       <?php if(isset($_SESSION['last_name_error'])) : ?> 
        <span class="error"><?php echo $_SESSION['last_name_error']; ?></span> 
       <?php unset($_SESSION['last_name_error']); endif; ?> 
      </div> 
      <div class="form-group"> 
       <input type="email" name="email" id="" placeholder="E-mail Address"> 
       <?php if(isset($_SESSION['email_error'])) : ?> 
        <span class="error"><?php echo $_SESSION['email_error']; ?></span> 
       <?php unset($_SESSION['email_error']); endif; ?> 
      </div> 
      <div class="form-group"> 
       <input type="email" name="confirm_email" id="" placeholder="Confirm E-mail Address"> 
       <?php if(isset($_SESSION['confirm_email_error'])) : ?> 
        <span class="error"><?php echo $_SESSION['confirm_email_error']; ?></span> 
       <?php unset($_SESSION['confirm_email_error']); endif; ?> 
      </div> 
      <div class="form-group"> 
       <input type="password" name="password" id="" placeholder="Password"> 
       <?php if(isset($_SESSION['password_error'])) : ?> 
        <span class="error"><?php echo $_SESSION['password_error']; ?></span> 
       <?php unset($_SESSION['password_error']); endif; ?> 
      </div> 
      <div class="form-group"> 
       <input type="password" name="confirm_password" id="" placeholder="Confirm Password"> 
       <?php if(isset($_SESSION['confirm_password_error'])) : ?> 
        <span class="error"><?php echo $_SESSION['confirm_password_error']; ?></span> 
       <?php unset($_SESSION['confirm_password_error']); endif; ?> 
      </div> 
      <div class="form-group"> 
       <input type="text" name="age" id="" placeholder="Age"> 
       <?php if(isset($_SESSION['age_error'])) : ?> 
        <span class="error"><?php echo $_SESSION['age_error']; ?></span> 
       <?php unset($_SESSION['age_error']); endif; ?> 
      </div> 
      <div class="form-group"> 
       <input type="submit" name="type" value="Register" class="register button"> 
      </div> 
     </form> 

和表單的PHP代碼是這樣的:

if ($_POST['type'] == 'Register') { 

    $i=0; 
    $names = array('first_name', 'last_name', 'email', 'confirm_email', 'password', 'confirm_password', 'age'); 
    foreach($names as $field) { 
     if (empty($_POST[$field])) { 

      $_SESSION[$field.'_error'] = "Field cannot be empty!"; 
      $i++; 
     } 

     else 
     { 
      unset($_SESSION[$field.'_error']); 
      $user->redirect('register.php'); 
     } 

    } 

    if (sizeof($names) == $i) { 
     $user->redirect('register.php'); 
    } 

    if ($_POST['email'] != $_POST['confirm_email']) { 
     $_SESSION['confirm_email_error'] = "E-mail Address does not match!"; 
     exit(); 
    } 

    if ($_POST['password'] != $_POST['confirm_password']) { 
     $_SESSION['confirm_password_error'] = "Password does not match!"; 
     exit(); 
    } 

    function add($region, $first_name, $last_name, $email, $confirm_email, $password, $confirm_password, $age) { 
     var_dump($_POST); 
     $database = Database::getInstance(); 
     $sql = "INSERT INTO `users` 
       (`region`, `first_name`, `last_name`, `email`, `confirm_email`, `password`, `confirm_password`, `age`) 
       VALUES (:region, :first_name, :last_name, :email, :confirm_email, :password, :confirm_password, :age)"; 

     $stmt = $database->pdo->prepare($sql); 
     $stmt->bindParam(':region', $region); 
     $stmt->bindParam(':first_name', $first_name); 
     $stmt->bindParam(':last_name', $last_name); 
     $stmt->bindParam(':email', $email); 
     $stmt->bindParam(':confirm_email', $confirm_email); 
     $stmt->bindParam(':password', $password); 
     $stmt->bindParam(':confirm_password', $confirm_password); 
     $stmt->bindParam(':age', $age); 
     $stmt->execute(); 

    } 

    $user->redirect(''); 

我還沒有填寫的$user->redirect('');,因爲我不知道應該在那裏什麼然而。

而對於thank_you_register網頁的HTML代碼是這樣的:

<?php require realpath(__DIR__ . '/header.php'); ?> 

<div class="main-content"> 
    <div class="messageBox"> 
     <div class="MB_head"> 
      <h2>Thank You!</h2> 
     </div> 
     <div class="MB_content"> 
      <p>You are registered.</p> 
     </div> 
    </div> 
</div> 
+0

有很多關於如何做自動重定向的教程。 – Epodax

+0

+0

您無法(合理地)使用PHP來做到這一點 - 您必須使用javascript 。 –

回答

0

最簡單的方法是使用JavaScript來做到這一點

window.location = "http://www.yoururl.com"; 
+0

使用location.replace不打破後退按鈕 – mplungjan

+0

你是什麼意思?你仍然可以推後退按鈕,並返回到您的網頁與window.location –

+0

如果我在「http://www.yoururl.com」;在你的腳本中,我會不斷推回到「http://www.yoururl。com「;如果我推後退按鈕 – mplungjan

1

您可以使用

<meta http-equiv="refresh" content="5; url=http://example.com/" /> 

或(通過javascript)

<script type="text/javascript"> 
    setTimeout(function() { 
     window.location.href = "http://example.com"; 
    },5000); 
</script> 
+0

使用location.replace不會中斷後退按鈕 - setTimeout – mplungjan

0

使用JavaScript做到這一點。它提供了更好的用戶體驗。

實施例:

<div class="main-content"> 
    <div class="messageBox"> 
     <div class="MB_head"> 
      <h2>Thank You!</h2> 
     </div> 
     <div class="MB_content"> 
      <p>You are registered.</p> 
     </div> 
     <div> 
      <p>Redirecting you back to the home page...click <a href="/url/to/redirect/to">here</a> if you are not automatically redirected</p> 
     </div> 
    </div> 
</div> 

<script> 
    setTimeout(function() { 
     window.location.href = "/url/to/redirect/to"; 
    }, 3000); // 3000 milliseconds until it calls function 

</script> 
1

在thank_you_register.php添加此腳本

<script>setTimeout(function() { location.replace("otherpage.html")},3000);</script> 

在3秒後,以取代thankyouregister。

注:我用location.replace不從thankyouregister

+0

我是對不起......通過「不破後退按鈕」,您想要的歷史路徑是從最後頁到原來的頁面(繞過'thank_you'),右 – shalvah

+0

是完全相同.... – mplungjan

0

您可以使用下面的PHP腳本打破後退按鈕來實現你上面

<?php 
     header('Location: http://www.example1.com/'); 
     sleep(20); 
     header('Location: http://www.example2.com/'); 

?> 
1

陳述一個簡單的解決方案:

在您的thank_you_register.php頁面把它放到您的<head>標記

<meta http-equiv="refresh" content="5; URL=http://your_site.com/"> 

5是秒。

+0

已經回答:HTTP: //stackoverflow.com/a/41591934/295783 – mplungjan