PHP表单必填字段错误消息未显示

时间:2015-02-18 17:47:56

标签: php

为什么浏览器窗口中没有显示错误消息? (有更多的字段,但都减少到最低) 没有任何事情发生,但它正在重定向到' save_entries.php'。 我本来希望表格中的错误信息是第一行,第二列。

代码:

<?php
session_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $errors   = array();
    if (empty($_POST['name']))
    {
        $errors['name'] = "Please insert your name!";
    }
    if (count($errors) == 0)
    {
        header("Location: save_entries.php");
        exit();
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link type="text/css" rel="stylesheet" href="css/stylesheet.css" />
    <title>Guestbook</title>
</head>

<body>
<form action="save_entries.php" method="POST">
    <div>
        <table class="entryForm">
            <tbody>
            <tr>
                <td></td>
                <td class="error">
                    <?php
                    if(isset($errorMeldung['name']))
                        echo $errorMeldung['name'];
                    ?>
                </td>
            </tr>
            <tr>
                <td class="labels">
                    <label for="name">Your Name:
                        <span>*</span>
                    </label>
                </td>
                <td class="fields">
                    <input type="text" name="name" id="name"
                           value="" maxlength="64" />
                </td>
            </tr>

            <tr>
                <td class="labels">
                </td>
                <td class="fields">
                    <input type="submit" class="submit button1"
                           value="Submit" />
                </td>
            </tr>
            </tbody>
        </table>
    </div>
</form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

问题是您要将表单发布到save_entries.php,因此问题中文件顶部的PHP永远不会运行。要解决此问题,请更改

<form action="save_entries.php" method="POST">

<form method="POST">

这会使表单回发到当前文件,而不是直接转到save_entries.php

答案 1 :(得分:0)

得到它:D(新的代码片段是粗体/ ** **)

<?php
    session_start();

	**$name = "";**
	
   $errors   = array();
   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $errors   = array();
            if (empty($_POST['name'])) 
            {
                $errors['name'] = "Please insert your name!";
            }  **else {
                $name = $_POST['name'];
                }** 

            if (count($errors) == 0)
            {
//'save_entries.php' gets the post entries, checks them (trim etc.) and save them into an array to give it back to the index.php to show the new guestbook entry with the old ones
                include_once 'save_entries.php';
            }
    }  
?>


<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" />
        <link type="text/css" rel="stylesheet" href="css/stylesheet.css" />
        <title></title>
    </head>

    <body>
                <form action="" method="POST">
                    <div>
                        <table class="entryForm">
                            <tbody>
                                <tr>
                                    <td></td>
                                    <td class="error">
                                        <?php 
                                            foreach ($errors as $error):
                                                echo $error . '<br />';
                                            endforeach;
                                        ?>
                                    </td>                                    
                                </tr>
                                <tr>
                                    <td class="labels">
                                        <label for="name" class="mandatory">Your Name:
                                            <span class="mandatory">*</span>
                                        </label>
                                    </td>
                                    <td class="fields">
                                        <input type="text" name="name" id="name" class="text mandatory" value="<?php **echo $name** ?>" maxlength="64" />
                                    </td>
                                </tr>
                                
                                <tr>
                                    <td class="labels">
                                    </td>
                                    <td class="fields">
                                        <input type="submit" class="submit button1" value="Submit" />
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                </form>
    </body>
</html>

相关问题