PHP显示发布变量

时间:2016-01-29 05:02:35

标签: php html forms

我在这里寻求一些帮助和一些解释。

我在POST输入时创建了一个带有几个输入字段和一些非常基本的PHP验证的HTML表单。我的验证只是检查字段是否有数据,如果没有,它会提示用户通过显示错误在字段中输入数据。我希望最终POST这些输入,针对数据库检查它们,如果它们不在那里,则将它们添加到数据库中。但是,这不是我的问题。

目前,我的目标是获取我想要POST的字段中的所有输入,并将其显示在错误显示区域下方的字段中。我曾希望只是回显数据但由于某种原因,并非所有输入的数据都出现。

在下面代码中的5个输入字段中,4个是基本输入字段,1个是文本区域。如果我在基本输入字段中输入任何内容,则只有最后一个输入将在我的显示区域中回显。如果我在字段1中输入内容并将其余部分留空,则会显示字段1。此外,如果我在文本区域输入内容,它将始终显示。最后,我的PHP验证似乎不适用于我的textarea类型输入(标记为' note')并且如果'注意'则不会返回错误。输入留空。任何人都可以解释:(1)如何修复它以便所有5个输入显示在显示div中? (2)为什么会发生这种情况? (3)如果文本区域(标记为' note')留空,为什么没有返回错误?

谢谢。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>

<?php
//form validation for general entry form

    // define variables and set to empty values
    $clientErr = $matterErr = $dateErr = $timeErr = $noteErr= "";
    $client = $matter = $date = $time = $note = "";


    //on post, check to see if variable is empty. if not empty
    //parse it and assign value back to variable name

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $complete = true;
        $postArray = [];


        if (empty($_POST["client"])) {
            $clientErr = "*A client name is required. ";
            $complete = false;
        }else {


            $client = test_input($_POST["client"]);
            //$postArray[] =  $client;
        }


        if (empty($_POST["matter"])) {
            $matterErr = "*A matter name is required. ";
            $complete = false;
        }else {
            $client = test_input($_POST["matter"]);
        }


        if (empty($_POST["date"])) {
            $dateErr = "*A date is required. ";
            $complete = false;
        }else {
            $client = test_input($_POST["date"]);
        }


        if (empty($_POST["time"])) {
            $timeErr = "*A time entry is required. ";
            $complete = false;
        }else {
            $client = test_input($_POST["time"]);
        }

        if (empty($_POST["note"])) {
            $noteErr = "*A note is required. ";
            $complete = false;
        } else {
            $note = test_input($_POST["note"]);
        }

    } 

    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }
?>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >

    <label for="client">Client:</label>
    <input type="text" placeholder = "Enter Client Name" name="client"> * 

    <label for="matter">Matter:</label>
    <input type="text" placeholder = "Enter Matter Name" name="matter"> *

    <label for="date">Date:</label>
    <input type="text" placeholder = "Enter Date" name="date"> *

    <label for="time">Time:</label> 
    <input type="text" placeholder ="Time to nearest tenth hour" name="time"> *

    <label for="note">Note:</label>
    <textarea name="note" placeholder ="Enter Any Notes" rows="4" cols="40"></textarea> 

<input type="submit" name="submit" value="Submit" class="submitbutton">

 </form>  


<div class="errorDiv">
    <?php 
        echo $clientErr;
        echo $matterErr; 
        echo $dateErr;
        echo $timeErr; 
    ?>      

</div>

<div class ="displayDiv">
    <?php

            echo "<h2>Your Input:</h2>";
            echo $client;
            echo "<br>";
            echo $matter;
            echo "<br>";
            echo $date;
            echo "<br>";
            echo $time;
            echo "<br>";
            echo $note;



    ?>
</div>


</body>
</html>

3 个答案:

答案 0 :(得分:5)

 <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>

    <?php
    //form validation for general entry form

        // define variables and set to empty values
        $clientErr = $matterErr = $dateErr = $timeErr = $noteErr= "";
        $client = $matter = $date = $time = $note = "";


        //on post, check to see if variable is empty. if not empty
        //parse it and assign value back to variable name

        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $complete = true;
            $postArray = [];


            if (empty($_POST["client"])) {
                $clientErr = "*A client name is required. ";
                $complete = false;
            }else {


                $client = test_input($_POST["client"]);
                //$postArray[] =  $client;
            }


            if (empty($_POST["matter"])) {
                $matterErr = "*A matter name is required. ";
                $complete = false;
            }else {
                $matter = test_input($_POST["matter"]);  //remove $client and assign value to $matter
            }


            if (empty($_POST["date"])) {
                $dateErr = "*A date is required. ";
                $complete = false;
            }else {
                $date = test_input($_POST["date"]); //remove $client and assign value to $date
            }


            if (empty($_POST["time"])) {
                $timeErr = "*A time entry is required. ";
                $complete = false;
            }else {
                $time = test_input($_POST["time"]); //remove $client and assign value to $time
            }

            if (empty($_POST["note"])) {
                $noteErr = "*A note is required. ";
                $complete = false;
            } else {
                $note = test_input($_POST["note"]);
            }

        } 

        function test_input($data) {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          return $data;
        }
    ?>


    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" >

        <label for="client">Client:</label>
        <input type="text" placeholder = "Enter Client Name" name="client"> * 

        <label for="matter">Matter:</label>
        <input type="text" placeholder = "Enter Matter Name" name="matter"> *

        <label for="date">Date:</label>
        <input type="text" placeholder = "Enter Date" name="date"> *

        <label for="time">Time:</label> 
        <input type="text" placeholder ="Time to nearest tenth hour" name="time"> *

        <label for="note">Note:</label>
        <textarea name="note" placeholder ="Enter Any Notes" rows="4" cols="40"></textarea> 

    <input type="submit" name="submit" value="Submit" class="submitbutton">

     </form>  


    <div class="errorDiv">
        <?php 
            echo $clientErr;
            echo $matterErr; 
            echo $dateErr;
            echo $timeErr; 
            echo $noteErr;  // echo noteerror here.........
        ?>      

    </div>

    <div class ="displayDiv">
        <?php

                echo "<h2>Your Input:</h2>";
                echo $client;
                echo "<br>";
                echo $matter;
                echo "<br>";
                echo $date;
                echo "<br>";
                echo $time;
                echo "<br>";
                echo $note;



        ?>
    </div>


    </body>
    </html>

在这里,您将所有值分配给单个变量$client并回显所有不同的变量,因此将值分配给特定变量,如$matter$date$time。你忘了在错误消息中回应$noteerror

答案 1 :(得分:4)

您复制并粘贴了else块,不是吗?在每个块中,您将值分配给$client,从而使用每个新的$_POST值覆盖它。将其他作业更改为$client$time或相应,然后尝试。

例如这个

$client = test_input($_POST["date"]);

应该是

$date = test_input($_POST["date"]);

答案 2 :(得分:2)

为什么要将所有值分配给相同的$ client变量?必须用不同的变量替换它。

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $complete = true;
    $postArray = [];


    if (empty($_POST["client"])) {
        $clientErr = "*A client name is required. ";
        $complete = false;
    }else {


        $client = test_input($_POST["client"]);
        //$postArray[] =  $client;
    }


    if (empty($_POST["matter"])) {
        $matterErr = "*A matter name is required. ";
        $complete = false;
    }else {
        $matter= test_input($_POST["matter"]);
    }


    if (empty($_POST["date"])) {
        $dateErr = "*A date is required. ";
        $complete = false;
    }else {
        $date= test_input($_POST["date"]);
    }


    if (empty($_POST["time"])) {
        $timeErr = "*A time entry is required. ";
        $complete = false;
    }else {
        $time= test_input($_POST["time"]);
    }

    if (empty($_POST["note"])) {
        $noteErr = "*A note is required. ";
        $complete = false;
    } else {
        $note = test_input($_POST["note"]);
    }