[英]Displaying error message in html form with php
我想通過 php 變量在我的 html 表單中顯示錯誤消息,這些變量填充了在“register-form_v2.php”中分配的值。
有人能告訴我為什么我想在表格中回顯的變量不包含任何值嗎? 因為當我提交表單時,我沒有向用戶顯示所需的錯誤消息,它只是將我定向到 if 語句中定義的 url,但僅此而已。
在帶有 html 表格的文件下方以及我想回顯的變量/錯誤消息。
<?php
include "register-form_v2.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<style>
#wrapper {
width: 100%;
margin: 0 auto;
background-color: aliceblue;
}
form {
display: flex;
flex-direction: column;
width: 50%;
margin: auto;
padding: 20px;
background-color: antiquewhite;
}
.input-wrapper {
margin: 20px;
font-size: 20px;
}
.red-text {
color: red;
}
</style>
<div id="wrapper">
<form action="register-form_v2.php" method="POST">
<div class="red-text" ><?php echo $errors['email'];?> Test, this displays :=)</div>
<div class="input-wrapper">
<label for="email">E-mail</label> <br>
<input id="email" type="text" name="email" value="">
</div>
<div class="red-text" ><?php echo $errors['username'];?></div>
<div class="input-wrapper">
<label for="username">Username</label> <br>
<input type="text" name="username" value="">
</div>
<div class="red-text" > <?php echo $errors['password']; ?></div>
<div class="input-wrapper" >
<label for="password">Password</label> <br>
<input type="text" name="password" value="">
</div>
<div class="red-text" > <?php echo $errors['password']; ?></div>
<div class="input-wrapper">
<label for="password-check">Password-check</label> <br>
<input type="text" name="password-check" value="">
</div>
<div class="input-wrapper">
<button type="submit" name="login-submit">Login</button>
</div>
</form>
</div>
</body>
</html>
帶有我的表單驗證的第二個文件。
<?php
$errors = array('fields' => '', 'email' => '', 'username' => '', 'password' => '');
$fields_err = '';
if($_SERVER["REQUEST_METHOD"] == "POST") {
require "dbConnect.php";
$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
$password_check = $_POST["password-check"];
if( empty($email) || empty($username) || empty($password) || empty($password_check)) {
header("location: SignUp.php?error=emptyfields&mail=".$email."&uid=".$username);
$errors['fields'] = 'You need to fill in the fields';
$fields_err = ' doh it went wrong no fields ';
echo 'asdfasdf';
}
//checking for valid email
elseif(!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$errors['email'] = 'please use a valid email and user ID, user ID can only use alphanummeric values';
header("location: SignUp.php?error=invalidmail_and_uid");
exit();
//cjecl
} elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'something not right there with your email budy';
header("location: SignUp.php?error=invalidmail&uid=".$username);
exit();
}
// checking for valid username
elseif(!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
$errors['username'] = 'your username is invalid, your username needs to include only numbers or letters';
header("location: SignUp.php?error=invalidUid&mail=".$email);
exit();
}
//checking for password length
elseif(strlen(trim($password)) < 11) {
$errors['password'] = 'your password must include at least 11 characters';
header("location: SignUp.php?error=nopswMatch&uid=".$username."&mail=".$email);
exit();
}
//checking if password and password_check match
elseif($password !== $password_check) {
$errors['password'] = 'your password confirmation does not match';
header("location: SignUp.php?error=nopswMatch&uid=".$username."&mail=".$email);
exit();
} else {
$sql = "SELECT username FROM dudes WHERE username=:username";
//check if username is already taken
if(!$stmt = $conn->prepare($sql)) {
//Bind variables to the prepared statemnet as parameters
header("location: SignUp.php?error=sqlerror");
exit();
} else {
$stmt->bindParam(":username", $username, PDO::PARAM_STR);
$stmt->execute();
$result_check = $stmt->rowCount();
if($result_check == 1) {
$errors['username'] = 'the user name has already been taken';
echo "The userName has already been taken";
}
else {
echo "great success";
// insert code here
$sql = "INSERT INTO dudes (email, username, password) VALUES (:email, :username, :password)";
if(!$stmt = $conn->prepare($sql)) {
header("location: SignUp.php?error=prepStmtFail");
} else {
$stmt->bindParam(':email', $email);
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $param_password);
// hash the password
$param_password = password_hash($password, PASSWORD_BCRYPT);
if($stmt->execute()){
header("location: SignUp.php?signup=success");
exit();
}
}
}
}
}
}
?>
您可以將錯誤臨時存儲在 $_SESSION 變量中。 或者您可以在 $_GET 變量中添加您的錯誤
例子:
// php part :
session_start();
$_SESSION['errors']['password'] = 'your password confirmation does not match';
header("location: SignUp.php?error=nopswMatch&uid=".$username."&mail=".$email);
// html part :
if(isset($_SESSION['errors'])) {
foreach ($_SESSION['errors'] as $key => $value) {
echo '<div class="red-text" >'. $_SESSION['errors']['email']. 'Test, this displays :=)</div>';
}
}
unset($_SESSION['errors']);
或者
// php part :
header("location: SignUp.php?error=your password confirmation does not match&uid=".$username."&mail=".$email);
// html part :
if(isset($_GET['error'])) {
echo '<div class="red-text" >'. $_GET['error']. 'Test, this displays :=)</div>';
}
變量值只能在單個頁面中使用。 由於您在另一個 php 文件中填充了 $error 數組,因此您不能在其他頁面中使用該變量。
您可以做的是使用 $_SESSION[] 變量,該變量可在代碼頂部使用 session_start() 的任何頁面中訪問。
或者在您的登錄或注冊頁面中使用它
<?php
if(isset($_GET['error']) && $_GET['error']=='emptyfield')
{
$errors['email'] = 'All field are compulsory";
}
?>
現在您可以在表單中使用 $errors['email']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.