PHP:显示来自表单验证的消息

时间:2015-10-19 14:19:39

标签: php html forms validation

您好,我有一个简单的表格和验证功能。 当我提交表单时,没有显示任何错误消息。我究竟做错了什么?是否有更好的解决方案来输出表单验证的错误消息?

<?php
include "functions.php";
?>

<html>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label>Username:</label>
<input type="text" name="username">
<br />
<label>Password:</label>
<input type="password" name="password">
<br />
<input type="submit" value="send">
</form>

<span><?php echo $nameErr ?></span>
<span><?php echo $pwErr ?></span>

</html>

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    validateForm();
}

?>

这是functions.php文件:

<?php

function validateForm()
{
    if (empty($_POST["username"])) 
    {
        $nameErr = "Name is required";
    }

    if (empty($_POST['password']))
    {
        $pwErr = "Password is required";
    }
}

?>

7 个答案:

答案 0 :(得分:0)

Functions.php

<?php

    function validateForm()
    {
        $errors = array();

        if (empty($_POST["username"])) 
        {
            $errors[] = "Name is required";
        }

        if (empty($_POST['password']))
        {
            $errors[] = "Password is required";
        }

        return '<span>'.implode('</span>,<span>', $errors).'</span>';
    }

    ?> 

main.php

<?php
include "functions.php";
?>

<html>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label>Username:</label>
<input type="text" name="username">
<br />
<label>Password:</label>
<input type="password" name="password">
<br />
<input type="submit" value="send">
</form>


<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    echo validateForm();    
}
?>
</html>

答案 1 :(得分:0)

在实例化之前,您必须从函数中声明$msg变量,并且在所有内容之上。我使用全局方法在函数内部调用它,现在我们可以在我们创建的函数内访问这个变量范围。

您的新include或要求 functions.php 文件应如下所示:

<?php
$msg = "";
function validateForm($name, $pwd){
    global $msg;    
    if(isset($_POST[$name]) && empty($_POST[$name])){
        $msg .= "Wrong name passed\n";  
    } elseif(isset($_POST[$pwd]) && empty($_POST[$pwd])){
        $msg .= "Wrong password\n"; 
    } else {
        $msg .= "Logged\n"; 
    }   
}
?>

html 内容和包含部分:

<?php
require_once 'functions.php';
if(isset($_SERVER["REQUEST_METHOD"]) && $_SERVER["REQUEST_METHOD"] === "POST"){
    validateForm('username','password');
}
?>
<html>
<form action="" method="POST">
<label>Username:</label>
<input type="text" name="username">
<br />
<label>Password:</label>
<input type="password" name="password">
<br />
<input type="submit" value="send">
</form>

<span><?php echo isset($msg) ? $msg : NULL; ?></span>

</html>

答案 2 :(得分:0)

您必须在$nameErr全局范围内创建变量$pwErrvalidateForm(),因为您想要echo函数之外的那些变量。函数内部的变量不具有全局范围。它只在内部功能。让你的变量像这样全局:

<?php

function validateForm(){
if (empty($_POST["username"])) 
{

   $nameEr = "Name is required";
   $GLOBALS['nameErr']=$nameEr;
}

if (empty($_POST['password']))
{

   $pwEr = "Password is required";
   $GLOBALS['pwErr']=$pwEr;
}
}

?>

还有回声

 <span><?php echo $nameErr ?></span>
 <span><?php echo $pwErr ?></span>
运行validateForm()函数后

答案 3 :(得分:-1)

您在显示错误消息后调用该功能。 在声明之后,在回声之前尝试调用它。

我也读过文森特的评论,它是真的,你要么必须宣称变量是全球性的,要么想到别的东西

答案 4 :(得分:-1)

我认为更好的东西:

function validateForm()
{
    var $errors = array();

    if (empty($_POST["username"])) 
    {
        $errors[] = "Name is required";
    }

    if (empty($_POST['password']))
    {
        $errors[] = "Password is required";
    }

    return '<span>'.implode('</span>,<span>', $errors).'</span>';
}

然后在您的视图中使用想要显示错误的位置:

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    echo validateForm();
}

答案 5 :(得分:-1)

<?php
include "functions.php";

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    validateForm();
}

?>

你必须在汇编你的html之前调用这个函数

答案 6 :(得分:-2)

如果您希望此脚本正常工作,验证代码需要在脚本的开头。

的functions.php

Address

HTML

function validateForm(&$nameErr, &$pwErr)
{
    if (empty($_POST["username"])) 
    {
        $nameErr = "Name is required";
    }

    if (empty($_POST['password']))
    {
        $pwErr = "Password is required";
    }
}