PHP表单未通过函数验证

时间:2014-02-18 15:03:42

标签: php html forms validation

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
<title>Guestbook</title>
<meta charset="ISO-8859-1">
</head>

<?php



function check($user, $email, $note, $userErr, $emailErr, $noteErr){
$userErr = $emailErr = $noteErr = "";
$user = $email = $note = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["user"])) 
        $userErr = "Please fill out a name.";
    else 
        $user = $_POST["user"];

    if (empty($_POST["email"]))  
        $emailErr = "Please fill out an email.";
    else 
        $email = $_POST["email"];

    if (empty($_POST["note"]))  
        $noteErr = "Please give us your comments.";
    else 
        $note= $_POST["note"];



}

    if ($userErr=="" or $emailErr=="" or $noteErr=="")
        display($user, $email, $note, $userErr, $emailErr, $noteErr);
        else
        displayResult($user, $email, $note);

}

function display($user, $email, $note, $userErr, $emailErr, $noteErr){
print<<<TABLE_BLOCK
<h2>Please Sign Our Guestbook</h2>
<form method="post" action="mock.php"> 
<table>
<tr>
    <td>Name:</td><td><input type="text" size="34" name="user" value=""  /><span class="error"><br> $userErr</span></td>
</tr>
<tr>
    <td>Email: </td><td><input type="text" size="34" name="email" value=""  /><span class="error"><br> $emailErr</span></td>
</tr>
<tr>
    <td valign="top">Comments: </td><td><textarea rows="5" cols="25" name="note"></textarea><span class="error"><br> $noteErr</span></td>
</tr>

<tr>
    <td></td><td></td>
</tr>
<tr>
    <td></td><td align="right"><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</form>
TABLE_BLOCK;
}

function displayResult($user, $email, $note){
print<<<TABLE_BLOCK
<h2>Your Input:</h2>
<table>
<tr>
<td>Name:</td><td>$user</td>
</tr>
<tr>
 <td>Email: </td><td>$email</td>
</tr>
<tr>
<td valgin="top">Comments: </td><td>$note</td>
</tr>
</table>
TABLE_BLOCK;
}



if(isset($_REQUEST['submit']))

    check($user, $email, $note, $userErr, $emailErr, $noteErr);
else
    display($user, $email, $note, $userErr, $emailErr, $noteErr);

?>



</body>
</html>

我已经知道错误存在于我的函数和/或我必须执行它们的逻辑中。但我真的不确定从这里到底要去哪里。在实现这些功能之前,一切都运行良好。当然,我是新手。按下提交按钮时,没有数据发送到displayResult()页面,并且只要表单提交完全空白,我的错误消息就不会弹出。这是我当前的页面:http://awsymposium.com/mock.php,最终产品的外观和操作应与此类似:http://professorgustin.com/dpr206/guestbook/guestbookonescript.php

1 个答案:

答案 0 :(得分:0)

我已经更新了一些你的代码。我还强烈要求你也为你的领域使用javascript验证。

以下是代码:

<!DOCTYPE HTML>
<html>
<head>
<style>
.error {color: #FF0000;}
</style>
<title>Guestbook</title>
<meta charset="ISO-8859-1">
</head>

<?php



function check($user, $email, $note){
$userErr = $emailErr = $noteErr = "";

    if ($user=="") 
        $userErr = "Please fill out a name.";

    else if ($email=="")  
        $emailErr = "Please fill out an email.";

    else if ($note=="")  
        $noteErr = "Please give us your comments.";

    if (($userErr !="") || ($emailErr !="") || ($noteErr !=""))
        display($user, $email, $note, $userErr, $emailErr, $noteErr);
    if(($userErr =="") && ($emailErr =="") && ($noteErr ==""))
        displayResult($user, $email, $note);

}

function display($user=null, $email=null, $note=null, $userErr=null, $emailErr=null, $noteErr=null){
print<<<TABLE_BLOCK
<h2>Please Sign Our Guestbook</h2>
<form method="POST" action="test321.php"> 
<table>
<tr>
    <td>Name:</td><td><input type="text" size="34" name="user" value="$user"  /><span class="error"><br> $userErr</span></td>
</tr>
<tr>
    <td>Email: </td><td><input type="text" size="34" name="email" value="$email"  /><span class="error"><br> $emailErr</span></td>
</tr>
<tr>
    <td valign="top">Comments: </td><td><textarea rows="5" cols="25" name="note">$note</textarea><span class="error"><br> $noteErr</span></td>
</tr>

<tr>
    <td></td><td></td>
</tr>
<tr>
    <td></td><td align="right"><input type="submit" name="submit" value="submit" /></td>
</tr>
</table>
</form>
TABLE_BLOCK;
}

function displayResult($user, $email, $note){
print<<<TABLE_BLOCK
<h2>Your Input:</h2>
<table>
<tr>
<td>Name:</td><td>$user</td>
</tr>
<tr>
 <td>Email: </td><td>$email</td>
</tr>
<tr>
<td valgin="top">Comments: </td><td>$note</td>
</tr>
</table>
TABLE_BLOCK;
}



if(isset($_REQUEST['submit']))

    check($_POST['user'], $_POST['email'], $_POST['note']);
else
    display();

?>



</body>
</html>