为什么此PHP代码无法验证用户输入?

时间:2019-02-05 22:58:44

标签: php html

我正在创建带有验证码的表单。我已经知道表单可以在我通过添加验证代码来构建先前项目的基础上工作。我正在上一堂课,我完全按照示例显示的进行操作。无论出于什么原因,它都没有验证。我不需要别人告诉我如何做功课,但是我真的可以在这里向正确的方向指出要点。

下面的代码来自我的HTML文件,当提交表单时,该数据通过PHP文件传递数据。这只是我苦苦挣扎的HTML文件中的验证代码,因此我不会附加PHP文件。

<!Doctype html>
<html>
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="basics.css"/>
<title>Form Process</title>
</head>
<body>
<div id="holder">
  <?php
  $fname=$_POST['fname'];
  $lname=$_POST['lname'];
  $street=$_POST['street'];
  $city=$_POST['city'];
  $state=$_POST['state'];
  $zip=$_POST['zip'];
  $phone=$_POST['phone'];
  if (empty($fname)) {
    $error_message="You must enter a value for your first name!";
  } else if (empty($lname)) {
    $error_message="You must enter a value for your last name!";
  } else if (empty($street)) {
    $error_message="You must enter a value for your street address!";
  } else if (empty($city)) {
    $error_message="You must enter a value for your city!";
  } else if (empty($state)) {
    $error_message="You must enter a value for your state!";
  } else if (empty($zip)) {
    $error_message="You must enter a value for your zip code!";
  } else if (empty($phone)) {
    $error_message="You must enter a value for your phone number!";
  } else {
    $error_message="You must enter a value!";
  }
?>
<h1>Sign Up Now!</h1>
<center><form action="info_output_includes.php" method="post">
<table>
<tr><td>First Name:</td><td><input type="text" id="fname" name="fname"/></td></tr>
<tr><td>Last Name:</td><td><input type="text" id="lname" name="lname"/></td></tr>
<tr><td>Address:</td><td><input type="text" id="street" name="street" /></td></tr>
<tr><td>City:</td><td><input type="text" id="city" name="city" /></td></tr>
<tr><td>State:</td><td><input type="text" id="state" name="state"/></td></tr>
<tr><td>Zip Code:</td><td><input type="text" id="zip" name="zip" /></td></tr>
<tr><td>Phone:</td><td><input type="text" id="phone" name="phone"/></td></tr>
<tr ><td ><input type="submit" value="Submit" /></td>
<td class="totheleft"><input type="reset" value="Clear" /></td></tr>
</table>
</form></center>
<br>
<a href="index.php">Home</a></p>
</div>
</body>
</html>

如果验证代码有效,则应显示(例如)“您必须输入名字的值!”用户提交表单时未输入名字值。当然,如果任何值保留为空,它基本上应该打印一条错误消息。

1 个答案:

答案 0 :(得分:1)

您没有看到该错误的原因是因为您没有将其输出到某个地方。您需要添加以下行:

<?php echo $error_message; ?>

仅使用HTML,以更合理的方式重组代码将是:

<?php
  $fname=$_POST['fname'];
  $lname=$_POST['lname'];
  $street=$_POST['street'];
  $city=$_POST['city'];
  $state=$_POST['state'];
  $zip=$_POST['zip'];
  $phone=$_POST['phone'];

  if (empty($fname)) {
    $error_message="You must enter a value for your first name!";
  } else if (empty($lname)) {
    $error_message="You must enter a value for your last name!";
  } else if (empty($street)) {
    $error_message="You must enter a value for your street address!";
  } else if (empty($city)) {
    $error_message="You must enter a value for your city!";
  } else if (empty($state)) {
    $error_message="You must enter a value for your state!";
  } else if (empty($zip)) {
    $error_message="You must enter a value for your zip code!";
  } else if (empty($phone)) {
    $error_message="You must enter a value for your phone number!";
  } else {
    $error_message="You must enter a value!";
  }
?>

<!DOCTYPE html>
<html lang="en" dir="lrt">
<head>
    <meta charset="utf-8" />
    <link rel="stylesheet" type="text/css" href="basics.css"/>
    <title>Form Process</title>
</head>
<body>
    <div id="holder">
        <h1>Sign Up Now!</h1>
        <div style="color:red;"><?php echo $error_message; ?></div>
        <center>
            <form name="input-form" action="info_output_includes.php" method="post">
                <table>
                <tr><td>First Name:</td><td><input type="text" id="fname" name="fname"/></td></tr>
                <tr><td>Last Name:</td><td><input type="text" id="lname" name="lname"/></td></tr>
                <tr><td>Address:</td><td><input type="text" id="street" name="street" /></td></tr>
                <tr><td>City:</td><td><input type="text" id="city" name="city" /></td></tr>
                <tr><td>State:</td><td><input type="text" id="state" name="state"/></td></tr>
                <tr><td>Zip Code:</td><td><input type="text" id="zip" name="zip" /></td></tr>
                <tr><td>Phone:</td><td><input type="text" id="phone" name="phone"/></td></tr>
                <tr ><td ><input type="submit" value="Submit" /></td>
                <td class="totheleft"><input type="reset" value="Clear" /></td></tr>
                </table>
            </form>
        </center>
        <br>
        <a href="index.php">Home</a></p>
    </div>
</body>
</html>