如何在表单输入为空时在表单输入旁边显示错误消息,以及如何在填充输入时删除错误消息?

时间:2019-05-25 21:58:57

标签: php

作为使用PHP作业的一部分,我有2个文件正在使用:

  1. application.php 和;
  2. processing.php

要求是用户提交申请表。如果输入字段为空,则错误消息应出现在输入字段旁边。用户将被重定向回同一应用程序页面,并且错误消息不应出现。

PHP代码只能在 processing.php 上,而HTML必须在 application.php 上。在application.php上允许使用一些最小的PHP,例如session_startechoprint,但是没有什么大不了的(例如大段PHP代码)。

经过几个小时,我尝试了以下方法,但无济于事:

  • empty();
  • !isset;
  • $_POST["fname"] = "";
  • session_start();
  • $row;
  • echo;
  • print;
  • unset()和;
  • = null

我无法使错误消息出现和消失。 PHP可以做到这一点吗?

application.php

<form id="application" method="post" action="processing.php" novalidate="novalidate">
    <fieldset>
        <p>
            <legend>Application Form</legend><p>
                <label for="fname">Name</label>
                <input type="text" name="fname" id="fname" required="required" />
        </p>
    </fieldset>
</form>

processing.php

<?php
    function sanitise_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
    require_once ("settings.php");
    $conn = @mysqli_connect(
      $host,
      $user,
      $pwd,
      $sql_db
    );
if ($conn) {
      if (isset($_POST["fname"])) {
        $fname = sanitise_input($_POST["fname"]);
      }
        else {
          mysqli_close($conn);
          header("location:application.php");
        }
else {
          mysqli_close($conn);
          header("location:application.php");
        }
      $query = "CREATE TABLE IF NOT EXISTS application (
        fname VARCHAR(50) NOT NULL)";
      $result = mysqli_query ($conn, $query);
      if (!$result) {
        echo "<p>Error!</p>";
      }
    }
    if (!$conn) {
      echo "<p>Failed!</p>";
    }
      else {
        $sql_table = "application";
        $query = "INSERT INTO $sql_table (fname) VALUES ('$fname')";
      $result = mysqli_query($conn, $query);
      if (!$result) {
        echo "<p>Error!", $query, "</p>";
      }
        else {
          $id = "SELECT * FROM application ORDER BY id DESC LIMIT 1";
          $idresult = mysqli_query($conn, $id);
          while ($row = mysqli_fetch_assoc($idresult)) {
            echo "<p>You are applicant #",$row["EOInumber"],"</p>";
          }
        }
      mysqli_close($conn);
    }
    ?>

2 个答案:

答案 0 :(得分:0)

这个问题不是很清楚,但是如果我要假设您要的是,如何在PHP中显示错误消息。您可以尝试:

如果(isset(变量名){

回显“显示错误:”

}

isEmpty(变量名) { 回显“显示错误:” }

尝试明确指定错误...

堆栈溢出中有多个答案,您可以搜索...

答案 1 :(得分:0)

根据您在注释ive中的输入来判断,提供以下代码。视需要修改

front.php

<?php
session_start();
?>
<html>
    <head>
        <title>Front Page</title>
    </head>
    <body>
        <h1 style="text-align:center;">My Form</h1>
        <div style="width: 500px;margin:0 auto; margin-top:5px">
            <form method="POST" action="back.php">
                <?php
                // Explode and convert errors string back into array so we can use methods like in_array()
                $errorsArray = [];

                if(isset($_SESSION['errors'])){
                    $errorsArray = explode(',', $_SESSION['errors']);
                }
                ?>
                <div style="margin-top:10px;">
                    <label for="name">Name</label>
                    <input type="text" id="name" name="name" placeholder="Enter a name" />
                    <?php
                        if(in_array('name', $errorsArray)){
                            echo '<div style="margin-top:10px;">Something wrong with name</div>';
                        }
                    ?>
                </div>
                <div style="margin-top:10px;">
                    <label for="email">Email</label>
                    <input type="text" id="email" name="email" placeholder="Enter a Email" />
                    <?php
                        if(in_array('email', $errorsArray)){
                            echo '<div style="margin-top:10px;">Something wrong with email</div>';
                        }
                    ?>
                </div>
                <div style="margin-top:10px;">
                    <button type="submit">Send</button>
                    <?php
                    if(isset($_SESSION['error']) && empty($_SESSION['status'])){
                        echo '<div style="margin-top:10px;">Some fields have errors</div>';
                    }

                    if(isset($_SESSION['status']) && empty($_SESSION['error'])){
                        echo '<div style="margin-top:10px;">Success</div>';
                    }
                    ?>
                </div>
            </form> 
        </div>
    </body>
</html>
<?php 
session_unset();
session_destroy();
?>

back.php

<?php
session_start();

$name = $_POST['name'];
$email = $_POST['email'];

if(isset($name) && isset($email) && $name != "" && $email != ""){
    // Continue with database insertion

    $_SESSION['status'] = 'success';
    echo 'Thanks'; // Echo your html
    header("refresh:5; url=./front.php"); // the 5 is seconds
    die();
} else {
    $errorsArray = [];

    if(!isset($name) || $name == ""){
        array_push($errorsArray, 'name');
    }

    if(!isset($email) || $email == ""){
        array_push($errorsArray, 'email');
    }

    $_SESSION['error'] = 'true';
    $_SESSION['errors'] = join(',', $errorsArray);
    header("Location:./front.php");
    die();
}
?>