PHP表单无法正确验证表单字段

时间:2019-03-14 08:21:17

标签: php html mysql sql forms

我正在学习更多PHP,并且在PHP本身可以工作之后,我似乎无法正确地验证任何表单字段。我的目标是检查firstname字段是否为空,如果为空,它将以红色显示一条消息。红色消息有效,但这仅是因为表单提交正在调用echo脚本,而不是因为它检测到任何空字段,因为当我做出else语句说“ wassup”(如果它不为空)时,我得到了当字段为空时,显示相同的消息。另外,有没有办法像JavaScript一样一次检查多个输入字段?例如,如果input1 ==''|| input2 =='',依此类推。这是我的HTML:

<html>
<head>
<title>Welcome</title>
</head>
<body>
<form action="welcome.php" method="post">
<fieldset>
<legend>Personal Info</legend>
First name <input name="name" type="text"> 
Middle name <input name="middlename" type="text"> 
Surname <input name="lastname" type="text">  
Age <input name="age" type="number"> 
Date of birth <input name="dob"  type="date">
</fieldset>
<fieldset>
<legend>Regional & location info</legend>
Continent 
<select>
<option value="europe">Europe</option>
<option value="americas">America</option>
<option value="africa">Africa</option>
<option value="asia">Asia</option>
<option value="australia">Australia</option>
<option value="eurasia">Eurasia</option>
</select>
Country <input name="country" type="text"> State <input type="text"> 
City <input name="city" type="text">
Street number <input name="streetno" type="number"> 
Street name <input name="streetname" type="text"> <br><br>
Suburb <input name="suburb" type="text"> Postcode <input name="postcode" type="number"> 
If none of these apply to your accommodations, enter a typed location here <input  type="text">
</fieldset>
<fieldset>
<legend>Previous lifestyle accommodations</legend>
Previous &/or most recent job title <input name="job" type="text"> 
First   time job seeker <input type="checkbox" name="check1" value="ftjb"> 
I'm a student <input type="checkbox" name="check2" value="ias"> 
Previous &/or most recent acedemic title <input name="school" type="text"> 
First time applying for a qualification <input type="checkbox" name="check3" value="ftafaq"> 
I have work experience <input type="checkbox" name="check4" value="ihwe">
</fieldset>
<fieldset>
<legend>Details of arrival</legend>
Reason for arrival <input name="reason" type="text"> 
Date of arrival <input name="arrival" type="date"> 
Amount of stay expectancy 
<input type="checkbox" name="check3">Temporary 
<input type="checkbox" name="check4">Longterm
</fieldset>
<fieldset>
<legend>Signiture</legend>
<input name='signiture' type="text"> 
</fieldset>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html> 

这是我的PHP代码:

<?php
$firstname = $_POST['name'];
$lastname = $_POST['lastname'];
$age = $_POST['age'];
$dob = $_POST['dob'];
$country = $_POST['country'];
$city = $_POST['city'];
$suburb = $_POST['suburb'];
$postcode = $_POST['postcode'];
$streetno = $_POST['streetno'];
$streetname = $_POST['streetname'];
$suburb = $_POST['suburb'];
$job = $_POST['job'];
$school = $_POST['school'];
$reason = $_POST['reason'];
$arrival = $_POST['arrival'];
$signiture = $_POST['signiture'];
if (isset($_POST['submit'])) {
    if (empty($_POST[$firstname])) {
        echo '<p style="color: red; text-align: center">Your first name is required</p>';
    } else {
        echo "wassaup";
    }
}





?>

6 个答案:

答案 0 :(得分:0)

将此<script> $(function() { $( "#to" ).datepicker({ defaultDate: "", maxDate:"+0d", changeMonth: false, numberOfMonths: 1, onClose: function( selectedDate ) { $( "#from" ).datepicker( "option", "minDate" ,selectedDate ); } }); }); </script> <script> $(function() { $( "#from" ).datepicker({ defaultDate: "", maxDate:"+0d", changeMonth: false, numberOfMonths: 1, onClose: function( selectedDate ) { $( "#to" ).datepicker( "option", "maxDate" ,selectedDate ); } }); }); </script> 更改为if (empty($_POST[$name])) {,然后再次检查。您的语法错误,这就是它不起作用的原因。

答案 1 :(得分:0)

在if语句中,您需要执行以下操作:

if (empty($_POST['name'])) {    //Or replace $_POST['name'] with $firstname
    echo '<p style="color: red; text-align: center">Your first name is required</p>';
} else {
    echo "wassaup";
}

答案 2 :(得分:0)

您使用了错误的语法if (empty($_POST[$firstname]))。您应该在firstname中使用''单一括号,并删除该符号$。

答案 3 :(得分:0)

更改代码

if (!isset($_POST['firstname'] || empty($_POST['firstname'])) {
    echo '<p style="color: red; text-align: center">Your first name is required</p>';
  } else {
    echo "wassaup";
 }

答案 4 :(得分:0)

首先,您可以创建错误数组,在其中放置所有错误

$error = array();

然后在您检查多个字段时

if (empty($_POST['firstname'])) $error[] = "First name can't be empty";
if (empty($_POST['lastname'])) $error[] = "Last name can't be empty";
// and so on

完成所有这些make语句后,检查错误数组是否为空,如果不显示错误,则显示为错误

if (empty($error)) {
   // do something
} else {
   // error exists you want to display all errors
   foreach ($error as $value) {
       echo '<ul><li>'.$value.'</li></ul>';
   }
}

答案 5 :(得分:0)

在html表单中,您必须添加html标记中所需的属性,而无需使用PHP处理

First name <input name="name" type="text" required="required"> 

如果用户未输入名字,他将收到错误消息(要求输入名字)