我正在爲使用PHP和MySQL的自己開發一個小項目。表單驗證顯示字段未完成
我已經創建了一個註冊表單,我一直在填寫它來測試它的工作原理。我在表單中添加了驗證來檢查是否有空字段,但即使我將數據輸入到每個字段中,它也會返回一個警告,說明並非所有字段都填充了,當它們顯然是。
下面是我的代碼:
<?php ob_start(); ?>
<?php require_once ("php/init.php"); ?>
<?php
$user = new User();
if (isset($_POST['submit']))
{
$user_username = trim($_POST['username']);
$user_email = trim($_POST['email']);
$user_firstname = trim($_POST['firstName']);
$user_lastname = trim($_POST['lastName']);
$user_password = trim($_POST['password']);
$verifyPassword = trim($_POST['passwordV']);
$user_company = trim($_POST['company']);
$validEmail = preg_match('/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z.]{2,4}$/', $user_email);
if ($user_username == ""
or $user_email == ""
or $user_firstname == ""
or $user_lastname == ""
or $user_password == ""
or $verifyPassword == ""
or $user_company == ""
)
{
$message = "You have not completed all the required fields, please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else
{
if ($validEmail)
{
if ($_POST['password'] != $_POST['passwordV'])
{
$message = "The passwords that you entered do not match, please try again.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else
{
$userFound = User::locate($user_username);
if ($userFound)
{
$message = "The username entered is already being used, please Login or enter a username.";
echo "<script type='text/javascript'>alert('$message');</script>";
}
else
{
$user_password = User::hash($user_password);
$user->user_username = $user_username;
$user->user_email = $user_email;
$user->user_firstname = $user_firstname;
$user->user_lastname = $user_lastname;
$user->user_password = $user_password;
$user->user_company= $user_company;
$user->insertUser();
redirect("wait.php");
}
}
}
else
{
$message = "The email address that was entered is not in the correct format";
echo "<script type='text/javascript'>alert('$message');</script>";
}
}
}
else
{
$user_username = "";
$user_email = "";
$user_firstname = "";
$user_lastname = "";
$user_password = "";
$verifyPassword = "";
$user_company = "";
}
if (isset($_POST['back']))
{
redirect('index.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<div id="register">
<p>Please enter the following details to register for an account.</p>
<p>All fields are required.</p>
<div class="input">
<li>
<form action="" method="post">
<p>
<label>Username:</label>
<input type="text" name="username">
</p>
<p>
<label>Email Address:</label>
<input type="text" name="email">
</p>
<p>
<label>First Name:</label>
<input type="text" name="firstName">
</p>
<p>
<label>Last Name:</label>
<input type="text" name="lastName">
</p>
<p>
<label>Password:</label>
<input type="password" name="password">
</p>
<p>
<label>Re-Enter Password:</label>
<input type="password" name="passwordV">
</p>
<p>
<label>Company:</label>
<input type="text" name="Company">
</p>
</br>
<p><input type="submit" name="submit"></p>
</br>
<p><input type="submit" name="back" value="Back"></p>
</form>
</li>
</div>
</div>
</body>
</html>
任何幫助,因爲我有點卡住將不勝感激。
不跳出來...你可以'var_dump'那個'if'阻止你檢查空嗎?很顯然,其中的一個是空的......不要相信$ _POST的轉儲......看看你的代碼正在評估什麼。 – ficuscr