为什么不是';这是验证用户输入的PHP代码吗?

为什么不是';这是验证用户输入的PHP代码吗?,php,html,Php,Html,我正在创建一个带有验证代码的表单。我已经知道,在我通过添加验证代码来构建之前的项目时,该表单是有效的。我是为一个班级做这件事的,我完全遵循了这个例子所展示的内容。不管出于什么原因,它都无法验证。我不需要有人告诉我如何做作业,但我真的可以在正确的方向上使用一个点 下面的代码来自我的HTML文件,在提交表单时,它通过PHP文件传递数据。这只是HTML文件中的验证代码,所以我不会附加PHP文件 <!Doctype html> <html> <head> <me

我正在创建一个带有验证代码的表单。我已经知道,在我通过添加验证代码来构建之前的项目时,该表单是有效的。我是为一个班级做这件事的,我完全遵循了这个例子所展示的内容。不管出于什么原因,它都无法验证。我不需要有人告诉我如何做作业,但我真的可以在正确的方向上使用一个点

下面的代码来自我的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>

形成过程
现在就报名吧!
名字:
姓氏:
地址:
城市:
声明:
邮政编码:
电话:


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

您没有看到错误的原因是您没有将其输出到某个地方。您需要添加一行,如:

<?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>

形成过程
现在就报名吧!
名字:
姓氏:
地址:
城市:
声明:
邮政编码:
电话:


@dean在添加验证代码之前,部分代码没有给我带来麻烦,尽管没有任何东西阻止它直接发布到info\u output\u includes.php。验证是在服务器端(php)进行的,因此在本例中,在提交表单之前不会对其进行验证。如果必须进行客户端验证,则可以使用javascript或html5。例如:。这将抛出一条html5必需的消息。您还需要在服务器端进行验证,以确保它是真正经过验证的。@user9189147嗯,我刚刚开始教授向我们展示的内容。我在这里删除了验证代码并将其移动到PHP文件中,它是否应该按照我所希望的方式工作?谢谢提示!我应该在哪里添加这样一行呢?在最初的PHP代码中?不,在HTML中的任何地方。例如,下面的
立即注册,您可以添加以下行:
。这应该指出HTML中的错误。我在上面添加了一个代码示例,代码正确缩进,添加了表单名称和错误输出。