我正在製作一個註冊表格,當您成功填寫所有字段時,它會重定向到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>
有很多關於如何做自動重定向的教程。 – Epodax
–
您無法(合理地)使用PHP來做到這一點 - 您必須使用javascript 。 –