我创建了一个带有确认密码的注册表单,但我的密码确认无效。我想在这里进行密码不匹配验证。
我哪里弄错了?请帮我解决这个问题。
<?php
$user=$country=$state=$city=$account=$email=$password="";
$userErr=$countryErr=$stateErr=$cityErr=$accountErr=$emailErr=$passwordErr="";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
if(empty($_POST["username"]))
{
$userErr="* Username is Required";
$valid=false;
}
else
{
$user=test_input($_POST["username"]);
}
if(empty($_POST["country"]))
{
$countryErr="* Country is required";
$valid=false;
}
else
{
$country=test_input($_POST["country"]);
}
if(empty($_POST["state"]))
{
$stateErr="* State is required";
$valid=false;
}
else
{
$state=test_input($_POST["state"]);
}
if(empty($_POST["city"]))
{
$cityErr="* City is required";
$valid=false;
}
else
{
$city=test_input($_POST["city"]);
}
if(empty($_POST["accounttype"]))
{
$accountErr="*AccountType is required";
$valid=false;
}
else
{
$account=test_input($_POST["accounttype"]);
}
if(empty($_POST["email"]))
{
$emailErr="* Email is required";
$valid=false;
}
else
{
$email=test_input($_POST["email"]);
}
if($_POST["password"] == $_POST["confirm_password"])
{
$passwordErr="* Password does not match";
$valid=false;
}
else
{
$password=test_input($_POST["password"]);
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="join_form1.php" method="post" enctype="multipart/form-data">
<tr>
<td width="163"><div align="right">User Name:</div></td>
<td width="409"><input name="username" type="text" value="<?php echo $user?>" /><?php echo $userErr?></td>
</tr>
<tr>
<td><div align="right">Country:</div></td>
<td><select name="country">
<option value="<?php echo $country ?>"><?php echo "$country"; ?></option>
<option value="Australia">Australia</option>
<option value="Canada">Canada</option>
<option value="Mexico">Mexico</option>
<option value="United Kingdom">United Kingdom</option>
<option value="United States">United States</option>
<option value="Zimbabwe">Zimbabwe</option>
</select><?php echo $countryErr?></td>
</tr>
<tr>
<td><div align="right">State: </div></td>
<td><input name="state" type="text" value="<?php echo $state ?>" /><?php echo $stateErr?></td>
</tr>
<tr>
<td><div align="right">City: </div></td>
<td>
<input name="city" type="text" value="<?php echo "$city"; ?>" /><?php echo $cityErr?>
</td>
</tr>
<tr>
<td><div align="right">Account Type: </div></td>
<td><select name="accounttype">
<option value="<?php echo $account ?>"></option>
<option value="a">Normal User</option>
<option value="b">Expert User</option>
<option value="c">Super User</option>
</select><?php echo $accountErr?></td>
</tr>
<tr>
<td><div align="right">Email: </div></td>
<td><input name="email" type="text" value="<?php echo $email ?>" /><?php echo $emailErr?></td>
</tr>
<tr>
<td><div align="right"> Password: </div></td>
<td><input name="password" type="password" value="<?php echo $password ?>" /><?php echo $passwordErr?><br />
<font size="-2" color="#006600">(letters or numbers only, no spaces no symbols)</font></td>
</tr>
<tr>
<td><div align="right"> Confirm Password: </div></td>
<td><input name="confirm_password" type="password" value="<?php echo $password ?>" /><?php echo $passwordErr?><br />
<font size="-2" color="#006600">(letters or numbers only, no spaces no symbols)</font></td>
</tr>
<tr>
<td><div align="right"> Captcha: </div></td>
<td>Add Captcha Here for security</td>
</tr>
<tr>
<td><div align="right"></div></td>
<td><input type="submit" name="Submit" value="Submit Form" /></td>
</tr>
</form>
答案 0 :(得分:0)
条件应为
if($_POST["password"] != $_POST["confirm_password"])
答案 1 :(得分:0)
你必须像这样检查
if($_POST["password"] != $_POST["confirm_password"])
{
$passwordErr="* Password does not match";
$valid=false;
}
else
{
$password=test_input($_POST["password"]);
}
答案 2 :(得分:0)
in your code if password and confirm password match then error is Password does not match";
if($_POST["password"] == $_POST["confirm_password"])
{
$passwordErr="* Password does not match";
$valid=false;
}
replace it
if($_POST["password"] != $_POST["confirm_password"])
{
$passwordErr="* Password does not match";
$valid=false;
}