PHP表格未正确验证表单字段



我正在学习更多的PHP,并且在PHP本身工作后,我似乎无法正确验证任何表单字段。我的目标是检查第一个名称字段是否为空,如果是的,它将以红色给出一条消息。红色作品中的消息,但这仅仅是因为echo脚本是通过表单提交来调用的,而不是因为它已经检测到了任何空字段,因为当我做出其他陈述时,如果不是空的,我就会说对于字段为空时,相同的消息。另外,是否可以像使用JavaScript一样一次检查多个输入字段?例如,如果输入1 ==''||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";
    }
}


?>

在您的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";
}

您的语法if (empty($_POST[$firstname]))错误。您应该在firstname中使用"单个派法",然后删除此标志$。

将此if (empty($_POST[$name])) {更改为if (empty($_POST['firstname'])) {,然后再次检查。您的语法是错误的,这就是为什么它不起作用。

在您的代码中更改

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

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

$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

完成所有这些制作声明后,以检查错误数组是否为空,如果不显示错误

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

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

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

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

最新更新