【问题标题】:PHP form not validating form field properlyPHP表单没有正确验证表单字段
【发布时间】:2019-03-14 08:21:17
【问题描述】:

我正在学习更多的 PHP,在 PHP 本身工作之后,我似乎无法让它正确验证任何表单字段。我的目标是检查名字字段是否为空,如果是,它将以红色显示消息。红色的消息有效,但只是因为表单提交正在调用 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";
    }
}





?>

【问题讨论】:

  • 我建议将 jquery 验证放在这里进行表单验证。
  • 请检查此线程:codepen.io/SitePoint/pen/akPoad
  • @prakashtank “我建议将 jquery 验证放在这里以进行表单验证” - 客户端验证永远不会替代服务器端验证,最多只是一个为方便用户选择额外的选项。
  • @04FS : 是的,我的意思是把验证客户端和服务器端都放在一起

标签: php html mysql sql forms


【解决方案1】:

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

【讨论】:

  • 好的,现在如何像我提到的那样一次检查多个字段?
  • 在后端还是前端?如果您要求使用 PHP,只需在 if 语句中添加下一个条件,例如:if (empty($_POST['name']) && empty($_POST['lastname']) 等等。
【解决方案2】:

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

【讨论】:

  • 它现在正在工作,我只需要弄清楚如何像我在问题中写的那样一次处理多个字段。
  • @Fico 我在 PHP 方面不是很好,但我会尝试看看我是否能做到。如果我的回答解决了您的第一个问题,请接受它作为正确答案。
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    更改您的代码

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

    【讨论】:

      【解决方案5】:

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

      $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>';
         }
      }
      

      【讨论】:

        【解决方案6】:

        在html表单中,需要在html标签中添加需要的属性,不使用PHP处理

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

        如果用户没有输入他的名字,他将收到错误消息(名字是必需的)

        【讨论】:

          猜你喜欢
          • 2013-06-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-05-18
          相关资源
          最近更新 更多