【发布时间】:2018-03-05 07:53:19
【问题描述】:
<?php
if($_SERVER["REQUEST_METHOD"] == "POST"){
$Fname = $_POST["Fname"];
$Lname = $_POST["Lname"];
$email = $_POST["email"];
$address1 = $_POST["address1"];
$address2 = $_POST["address2"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip_code = $_POST["zip"];
$phone = $_POST["phone"];
$distance = $_POST["distance"];
//form php validation error massage for blank fields
if($Fname == "" || $Lname == "" || $email == "" || $address1 == "" || $city == "" || $state == "" || $zip_code == "" || $phone == ""){
$msg = "<p class='error'>The required fields cannot be blank</p>"
;
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) && !isset($msg)){ //email validation
$msg = "<p class='error'>You must place a real email address</p>";
}else{
require_once("inc/admin_login.php"); //log in database file
$query = "INSERT INTO runner(fname,lname,email,address1,address2,city,state,postcode,phone,distance) VALUES('$Fname','$Lname','$email','$address1','$address2','$city','$state','$zip_code','$phone','$distance')";
$result=mysqli_query($con,$query);
if($result){ //if successfull logining in to database message
$msg = "<h2 class='notice'>Thank You For Signing Up</h2>";
}else{
$error_message = mysqli_error($con);
$result = "There was an error: $error_message";
exit($result);
}// end if
}//end else
header("Location:signup.php?status=thankyou"); //redicrects you when comments are entered in correctly
}// end of main 1st if
$pageTitle="Sign Up";
include("inc/header.php");
include("inc/navigation.php");
?>
<div class="signupWrapper">
<h1>Sign up to run</h1>
<?php if(isset($msg)){ //where blank error message will display
echo $msg;
}if(isset($_GET['status']) && $_GET['status'] == "thankyou"){ // msg that displays on the redirect page if comments are entered correctly
echo "<p class='notice'>Thank you for submitting your comment</p>";
}else{
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="signUpForm" name="signUpForm" title="Sign To Race Form">
<table class="signUp">
<tr>
<th>
<label for="Fname">First Name:</label>
</th>
<td>
<input type="text" id="Fname" name="Fname"></input>
</td>
</tr>
<tr>
<th>
<label for="Lname">Last Name:</label>
</th>
<td>
<input type="text" id="Lname" name="Lname"></input>
</td>
</tr>
<tr>
<th>
<label for="email">Email:</label>
</th>
<td>
<input type="email" id="email" name="email"></input>
</td>
</tr>
<tr>
<th>
<label for="address1">Address 1:</label>
</th>
<td>
<input type="text" id="address1" name="address1"></input>
</td>
</tr>
<tr>
<th>
<label for="address2">Address 2:</label>
</th>
<td>
<input type="text" id="address2" name="address2"></input>
</td>
</tr>
<tr>
<th>
<label for="city">City:</label>
</th>
<td>
<input type="text" id="city" name="city"></input>
</td>
</tr>
<tr>
<th>
<label for="state">State:</label>
</th>
<td>
<input type="text" id="state" name="state"></input>
</td>
</tr>
<tr>
<th>
<label for="zip">Zip Code:</label>
</th>
<td>
<input type="text" id="zip" name="zip"></input>
</td>
</tr>
<tr>
<th>
<label for="zip">Phone:</label>
</th>
<td>
<input type="text" id="phone" name="phone" title="enter phone number"></input>
</td>
</tr>
<tr>
<th>
<label for="">Distance:</label>
</th>
<td>
<fieldset>
<label for="distance1">1 Mile</label><input type="radio" id="distance1" name="distance" value="1mile" ></input>
<label for="distance2">5K</label><input type="radio" id="distance2" name="distance" value="5K" checked></input>
<label for="distance3">10K</label> <input type="radio" id="distance3" name="distance" value="10K" ></input>
</fieldset>
</td>
</tr>
<tr>
<th>
</th>
<td>
<label for=""></label><input type="submit" id="submitForm" name="submitForm" value="Submit" title="submit button"></input>
<label for=""></label><input type="reset" id="clear" name="clear" value="Clear" title="clear button" ></input>
</td>
</tr>
</table>
</form>
<?php }?> <!-- ends the else statement above in the start of the form, to only show the thank you message/ thank redirect page if
comments are entered in correctly -->
</div>
【问题讨论】:
-
我没有发现错误,但您应该阅读stackoverflow.com/questions/60174/…
-
无论是否有任何字段为空,提交按钮都会执行此操作。您可以在提交表单之前使用 javascript 检查字段 -
onsubmit等 -
使用
is_null或is_empty以及==""来验证它们是否为空 -
到底是什么问题?如果禁用 JS,则单击提交按钮时会提交表单。没有解决方法,因为没有其他东西会阻止提交。但是您可以查看表单字段的
required属性 -
我试图用 PHP 验证服务器端的表单,以防止空白字段被上传到 MySql 数据库中。因此,当表单上的字段为空白时,我希望显示错误消息,因此不会提交带有空白字段的表单。
标签: php forms validation