您好我有一个奇怪的问题。我有一个PHP验证表单,但即使表单字段为空,表单仍然提交。以下是我的代码......你能否告诉我这里发生了什么。
同一页面上还有2个表格。两者都使用$_SERVER['PHP_SELF']
动作。
工作流程是用户使用第一个表单旋转奖品轮(验证在此表单上正常工作),然后在成功提交后,微调器出现。
如果微调器落在获胜段上,则第二个表单出现(哪个将提交为空)如果我在第二个表单上单击提交,则第一个表单再次出现并忽略第二个表单验证。我确定可以安全地假设这是因为页面令人耳目一新。有办法解决这个问题吗?
$nameErr = $lNameErr = $pCodeErr = $successMessage = $errorMessage = "";
$name = $lName = $pCode = "";
$errors = 0;
$regex = '^[a-zA-Z0-9]{3}( )?[a-zA-Z0-9]{3}$^';
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "First Name is required";
$errors++;
}else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["lName"])) {
$lNameErr = "Last Name is required";
$errors++;
}else {
$lName = test_input($_POST["lName"]);
}
if (empty($_POST["pCode"])) {
$pCodeErr = "Postal Code is required";
$errors++;
}else {
$pCode = test_input($_POST["pCode"]);
// check if e-mail address is well-formed
if (!preg_match($regex, $_POST["pCode"])) {
$pCodeErr = "Invalid Postal Code format";
$errors++;
}
}
if($errors == 0){
$duplicate = $conn->prepare( "SELECT `Pcode` FROM `winners` WHERE `Pcode` = ?" );
$query->bindValue( 1, $email );
$query->execute();
if( $duplicate->rowCount() > 0 ) {
$errorMessage = "Sorry this user is already in our database!";
} else {
$successMessage = "Form Submission Successful!";
$sql = "INSERT INTO winners (Fname, Lname, Pcode) VALUES (:fName, :lName, :pCode)";
$stmt = $conn->prepare($sql);
// $stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':fName', $name, PDO::PARAM_STR);
$stmt->bindParam(':lName', $lName, PDO::PARAM_STR);
$stmt->bindParam(':pCode', $pCode, PDO::PARAM_STR);
$stmt->execute();
}
}
}
这是我的表格
<form id="winner-form" class="spin-form" method = "post" action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<p>To claim your prize please fill in the form below.</p>
<div class="form-group">
<label>Name:</label>
<input class="form-control" placeholder="Please enter your First Name" value="<?php echo $name;?>" type = "text" name = "name">
<span class = "error">* <?php echo $nameErr;?></span>
</div>
<div class="form-group">
<label>Last Name:</label>
<input class="form-control" placeholder="Please enter your Last Name" value="<?php echo $lName;?>" type = "text" name = "lName">
<span class="error">* <?php echo $lNameErr;?></span>
</div>
<div class="form-group">
<label>Postal Code:</label>
<input class="form-control" placeholder="Please enter your Postal Code" value="<?php echo $pCode;?>" type = "text" name = "pCode">
<span class="error">* <?php echo $pCodeErr;?></span>
</div>
<td>
<input class="btn btn-lg btn-success" type="submit" name="sub" value="Submit">
</td>
<?php if($successMessage != "") { ?>
<span class ='alert alert-success'><?php echo $successMessage; ?></span>
<?php } ?>
<?php if($errorMessage != "") { ?>
<span class ='alert alert-danger'><?php echo $errorMessage; ?></span>
<?php } ?>
</form>
答案 0 :(得分:0)
抱歉,我无法发表评论,因为我的声誉较低,如果您将SQL Null检查设置为NO,则可以阻止用户提交空值。或者在Php的结尾。
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["name"]) == "") {
$nameErr = "First Name is required";
$errors++;
}
}
或
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if ($_POST["name"]) == Null) {
$nameErr = "First Name is required";
$errors++;
}
}
但使用Javascript或Jquery编写表单验证函数通常是最好的。你喜欢哪个。我希望我的假设评论很有帮助。 :)