繁体   English   中英

PHP表单未使用函数进行验证

[英]PHP form not validating with functions

<!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

我已经更新了一点您的代码。 我也强烈建议您也对字段使用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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM