我對如何在PHP中實現Post/Redirect/Get模式有點困惑。我見過幾個答案,只是在提交表單後添加標題函數;我已經這樣做了,但在驗證輸入字段是否爲空時,它不會打印任何內容,儘管我在運行代碼後添加了頭部函數。我根本找不到如何整合這個,所以我反正問。如果輸入字段爲空,PHP將不會回顯;與PRG模式
<?php
require '../BACKEND/DB/DB_CONN.php';
if(isset($_POST['submit-register'])) {
if(empty($_POST['email'])) {
echo 'Email cannot be nothing.';
}
header("Location: index.php");
die();
}
if(isset($_POST['submit-login'])) {
if(empty($_POST['loginUser'])) {
echo 'Field Empty.';
}
header("Location: index.php");
die();
}
?>
<div class="forms">
<form method="post" action="index.php" role="form" class="forms-inline register ">
<h4>Register</h4>
<input type="text" name="email" placeholder="Email Address" autocomplete="off" />
<input type="text" name="username" placeholder="Username" autocomplete="off" />
<input type="password" name="password" placeholder="Password" autocomplete="off" />
<input type="submit" name="submit-register" value="Register" role="button" name="login-btn" />
</form>
<form method="post" action="index.php" role="form" class="forms-inline login ">
<h4>Login</h4>
<input type="text" name="loginUser" placeholder="Username" />
<input type="password" name="loginPass" placeholder="Password" />
<input type="submit" name="submit-login" value="Register" role="button" />
</form>
</div>
我在寫代碼,但是我發現它不工作,我問如何解決這一問題,以及如何實現郵政/重定向/模式安全,我知道有用。
如果有任何信息打印到頁面,則標題無法正常工作。另外,如果頭文件正在工作,您將永遠不會看到該輸出,因爲它會在實際加載頁面之前重定向。 – GrumpyCrouton
@GrumpyCrouton我甚至在打印之前放置了標題,沒有打印任何東西。 – Adam