在PHP中显示错误消息的更好方法是什么?

时间:2017-04-25 09:53:39

标签: php

我创建了三个php表单。第一个(form.php)包含表单标记,第二个(form1.php)         是信息的所在。最后一个(formerror.php),我创建的时候,我找不到方法         在第一页(form.php)上显示错误消息。我想知道的是,这样做是否合适?          - 创建与第一页完全相同的页面,以便显示错误消息。

enter image description here

这是第一页。

enter image description here

这是我必须完全复制第一页才能显示错误信息的最后一页。

第一页有一个表格,我从中输入信息并在第二页中发送,即form1.php

    CODE FROM THE FIRST PAGE(form.php):
<!DOCTYPE html>
<html>
<head>
    <title>Form</title>
    <style type="text/css">
        input {
            display: block;
            margin: 10px;
            padding: 10px;
        }
        form {
            border: 0px solid rgba(0,0,0,0.5);
            width: 19%;
            padding: 20px;
            background-color: rgba(5,25,9,0.2);
        }
    </style>
</head>
<body>
//This is the form from form.php
    <form action="form1.php" method="post">
        <input type="text" name="username" placeholder="Enter username">
        <input type="email" name="email" placeholder="Enter email address">
        <input type="password" name="password" placeholder="Enter password">
        <input type="password" name="cpassword" placeholder="Enter password again">
        <input type="submit" name="submit_btn" value="Submit">
        <input type="reset">
    </form>

    <p style="color: red;" class="erreur"></p>


    </body>
    </html>

    //This is the second page

从form.php输入一次信息后,来到此处并显示,因为我们已经提供了用户名和电子邮件地址         第二页的代码(form1.php):

    <!DOCTYPE html>
    <html>
    <head>
        <title>FORM 1</title>
    </head>
    <body>

    <?php
        $username = $_POST['username'];
        $email = $_POST['email'];
        $submit_btn = $_POST['submit_btn'];
        $password = $_POST['password'];
        $cpassword = $_POST['cpassword'];

        if (isset($_POST['submit_btn'])) {
            if ((empty($_POST['username'])) || (empty($_POST['email'])) || (empty($_POST['password'])) || (empty($_POST['cpassword']))) {
                // header("Location:form.php");
                header("Location:formerror.php");
            }else{
                echo "Welcome $username, your email address is $email";
            }
        }

    ?>



    </body>
    </html>

这是我必须创建的页面才能显示错误。我所做的是,我复制了与form.php完全相同的页面,但在底部,我添加了一条消息,如图所示。

    AND CODE FROM THE THIRD PAGE(formerror.php): 

    <!DOCTYPE html>
    <html>
    <head>
        <title>Form</title>
        <style type="text/css">
            input {
                display: block;
                margin: 10px;
                padding: 10px;
            }
            form {
                border: 0px solid rgba(0,0,0,0.5);
                width: 19%;
                padding: 20px;
                background-color: rgba(5,25,9,0.2);
            }
        </style>
    </head>
    <body>

    <form action="form1.php" method="post">
        <input type="text" name="username" placeholder="Enter username">
        <input type="email" name="email" placeholder="Enter email address">
        <input type="password" name="password" placeholder="Enter password">
        <input type="password" name="cpassword" placeholder="Enter password again">
        <input type="submit" name="submit_btn" value="Submit">
        <input type="reset">
    </form>

    <p style="color: red;">Please fill the form</p>


    </body>
    </html>

正如你所看到的那样,页面就在那里。我还想知道我的代码是否做得好,尽管它们完美无缺。

2 个答案:

答案 0 :(得分:0)

试试这个。放在你的php文件上面。

$server = "localhost";
$username= "root";
$pass = "";
$dbname ="";
$conn = new mysqli($server, $username, $pass, $dbname);

if($conn -> connect_error){
    echo "Connection Failed!, " . $conn -> connect_error;
}

答案 1 :(得分:0)

您只能使用一个文件来解决问题。将表单数据发送到同一页面。 像往常一样在文件开头之前添加PHP脚本。

<?php        
    $error = '';
    if (isset($_POST['submit_btn'])) 
    {
        $server = "localhost";
        $username= "";    // your username
        $pass = "";    // your password
        $dbname ="";   // your database name
        $conn = new mysqli($server, $username, $pass, $dbname);
        if($conn -> connect_error){
            echo "Connection Failed!, " . $conn -> connect_error;

        if(checkValidation())
            // insert the data into the database and redirect to a new page.                     
    }

    function checkValidation()
    {
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $cpassword = $_POST['cpassword'];
        if(empty($username))
        {
            $error = "Username field can't be empty";      
            return false;                    
        }
        if(empty($email))
        {
            $error = "Email field can't be empty";    
            return false;                      
        }
        if(empty($password))
        {
            $error = "Password can't be empty";                          
            return false;
        }
        if(empty($cpassword) || ($password != $cpassword))
        {
            $error = "Passwords don't match";                          
            return false;
        }
            return true;
    }
?>

然后添加你的HTML代码。

<html>
<head>
    <title>Form</title>
    <style type="text/css">
        input {
            display: block;
            margin: 10px;
            padding: 10px;
        }
        form {
            border: 0px solid rgba(0,0,0,0.5);
            width: 19%;
            padding: 20px;
            background-color: rgba(5,25,9,0.2);
        }
    </style>
</head>
<body>
<?php>
    if(!empty($error))
    {
        echo "<p style='color: red;'></p>";
    }
?>
<form action="" method="post">
    <input type="text" name="username" placeholder="Enter username">
    <input type="email" name="email" placeholder="Enter email address">
    <input type="password" name="password" placeholder="Enter password">
    <input type="password" name="cpassword" placeholder="Enter password again">
    <input type="submit" name="submit_btn" value="Submit">
    <input type="reset">
</form>
</body>
</html>