使用PHP函数验证表单

时间:2014-02-18 03:25:45

标签: php html forms function validation

以下代码在我将它们放入函数之前完美地工作但我无法弄清楚如何使用我创建的函数使这个表单正常工作。我知道我需要传递变量并创建一些正确的主逻辑,但我真的不知道从哪里开始。最终产品应该看起来像这样的形式:guestbookonescript

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

<?php

function check(){
$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"];



}
}       


function display(){
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 result(){
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();
else
    display();
    result();




?>



</body>

2 个答案:

答案 0 :(得分:1)

Alon试图说的是所有变量都在本地范围内捕获,为避免这种情况,您需要告诉它们属于全局范围的有问题的变量。从技术上讲,您不需要先将它们初始化,但这是一种很好的做法。

注意,您需要确保您的变量位于您正在使用它们的每个函数的全局范围内。

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

<?php

$userErr = $emailErr = $noteErr = "";
$user = $email = $note = "";

function check(){

    global $user, $email, $note;
    global $userErr, $emailErr, $noteErr;


    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"];

    }
}       


function display(){

    global $userErr, $emailErr, $noteErr;

print<<<TABLE_BLOCK
<h2>Please Sign Our Guestbook</h2>
<form method="post" action="/"> 
<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 result(){

    global $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();

display();
result();

?>

</body>

答案 1 :(得分:-1)

您需要将在函数外声明的变量定义为全局变量。在function result(){

之后,将此行放在函数的开头
global $user,$email,$note;

请注意,在函数执行后,函数作用域内声明的变量将被删除。您需要声明$user,$email,$note ouside check()(并在check()内声明它们是全局的)