表中包含错误消息的必填字段

时间:2018-04-04 10:26:09

标签: php html forms

对于作业,我正在制作一个带有php脚本的表单,该脚本在浏览器的表格中显示输入的数据。我已完成此任务,但现在我必须制作php脚本,以便在任何字段留空时不允许执行脚本。因此,我必须确保填写所有$ _POST变量,并在用户尝试使用空字段运行html时向用户提供错误消息。

继承我的HTML代码

<html>
<head>
<style>
body {
    margin-left: 15%;
    background-image: url("https://i.ytimg.com/vi/C6yVfvhYLPE/maxresdefault.jpg");
    background-repeat: no-repeat;
}
h1 {
    color: #000000;
    margin-left: -15%;
    text-align: center;
}

p {
    font-family: Helvetica;
    font-size: 22px;
}

table {
    font-family: Helvetica;
}
.error {color: #FF0000;
font-size: 14px;}
</style>
</head>
 <body>
 <h1>Contact form</h1>
 <p>Please fill out the contact form below.<br>
 <span class="error">* required field.</span></p>
   <form action="process_data2.php" method="POST" id="form2">
    <table>
        <tr>
            <td>First name: </td><td><input type="text" name="firstname" required>
            <span class="error">* <?php echo $fnErr;?></span></td>
        </tr>
        <tr>
            <td>Last name: </td><td><input type="text" name="lastname">
            <span class="error">* <?php echo $lnErr;?></span></td>
        </tr>
        <tr>
            <td>Address: </td><td><input type="text" name="address">
            <span class="error">* <?php echo $adErr;?></span></td>
        </tr>
        <tr>
            <td>Phone: </td><td><input type="text" name="phone">
            <span class="error">* <?php echo $phErr;?></span></td>
        </tr>
        <tr>
            <td>Comments: </td><td><textarea name="comments"
            form="form2" rows="6" cols="50"></textarea><span class="error">* <?php echo $coErr;?></span></tr>
        </tr>
    </table>
    <input type="submit" value="Submit button"/>
   </form>
 </body>
</html>

继承人的PHP

<html>
<style type="text/css">
body {
    background-image: url("https://i.ytimg.com/vi/C6yVfvhYLPE/maxresdefault.jpg");
    background-repeat: no-repeat;
}
h1 {
    color: #000000;
    text-align: center;
}

p {
    font-family: Helvetica;
    font-size: 22px;
    margin-left: 15%;
}

table {
    font-family: Helvetica;
    width: 70%;
    margin-left: 15%;
    margin-right: 15%;
}

th {
font-family: Helvetica;
font-size: 20;
color: #000000;
border: 2px solid #CD853F;
}

td {
font-family: Helvetica;
font-size: 18;
color: #000000;
border: 2px solid #CD853F;
}
</style>
<body>
<h1>Results</h1>
<p>Find your details in the table below.</p>
</body>
</html>

<?php
$fnErr = $lnErr = $adErr = $phErr = $coErr = "";
$firstname = $lastname = $address = $phone = $comments = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["firstname"])) {
    $fnErr = "First name is required";
  } else {
    $firstname = $_POST["firstname"];
  }

  if (empty($_POST["lastname"])) {
    $lnErr = "Last name is required";
  } else {
    $lastname = $_POST["lastname"];
  }

  if (empty($_POST["address"])) {
    $adErr = "Address is required";
  } else {
    $address = $_POST["address"];
  }

  if (empty($_POST["phone"])) {
    $phErr = "Phone number is required";
  } else {
    $phone = $_POST["phone"];
  }

  if (empty($_POST["comments"])) {
    $coErr = "The comments field is required";
  } else {
    $comments = $_POST["comments"];
  }
}
    echo "<table>";
        echo "<tr>";
            echo "<th>".'First Name'."</td>";
            echo "<th>".'Last Name'."</th>";
            echo "<th>".'Address'."</th>";
            echo "<th>".'Phone'."</th>";
            echo "<th>".'Comments'."</th>";
        echo "</tr>";
        echo "<tr>";
            echo "<td>".$_POST['firstname']."</td>";
            echo "<td>".$_POST['lastname']."</td>";
            echo "<td>".$_POST['address']."</td>";
            echo "<td>".$_POST['phone']."</td>";
            echo "<td>".$_POST['comments']."</td>";
        echo "</tr>";
    echo "</table>";
?>

1 个答案:

答案 0 :(得分:0)

假设您不想在此处使用JavaScript,您可以使用会话来保存错误并重定向以将您的错误带到第一页。

第2页示例:

$error = array();
if (empty($_POST["firstname"])) {
    $error[] = 'First name required'; 
  } else {
    $firstname = $_POST["firstname"];
  }

  if (empty($_POST["lastname"])) {
    $error[] = 'Last name required';
  } else {
    $lastname = $_POST["lastname"];
  }
etc...

然后,您可以检查是否找到任何错误

if (!empty($error)) {
    session_start();
    $_SESSION['ERROR'] = $error;
    header( 'Location: {path to page 1}');
}

返回第1页,检查会话是否已设置,如果是,则检查数组。

<?php 
session_start();
if (!empty($_SESSION['ERROR'])) {
    $errors = $_SESSION['ERROR'];
    unset($_SESSION['ERROR']);
    foreach ($errors as $error) {
        echo "<span class=\"error\">" . $error . "</span>";
    }
}
?>

这将显示每个错误的span元素。