簡體   English   中英

使用PHP驗證表單時出現問題

[英]Problems Validating Form With PHP

我正在為我的網站編寫一份簡短的聯系表,但遇到一些問題。 一切都可以正常顯示,但是除了未定義變量的通知(因為我使用$ post [$ value]將數據保留在表單輸入中)之外,驗證完全不再起作用。 我已經將其與之前編寫的功能代碼進行了比較,但是沒有發現任何明顯的不同。 這是表格的代碼:

<?php
$labels = array("first_name" => "First Name:",
                "last_name" => "Last Name:",
                "company" => "Company*:",
                "email" => "Email:",
                "phone" => "Phone Number*:",
                "subject" => "Subject:",
                "message" => "Message:");
$submit = "Submit";
?>

...

<?php
echo $error_message;
?>
<form method="post">
<table class="contact">
<?php
foreach($labels as $label => $field)
{
    if($label != "message")
    {
        echo "<tr><td class='label'>$field</td>
        <td class='input'><input type='text' maxlength='64' value='".$_POST[$label]."'></td>
        <td class='error'>$error_array[$label]</td></tr>";
    }
    else {
        echo "<tr><td class='label'>$field</td>
        <td class='input'><textarea cols='25' rows='6'>".$_POST[$label]."</textarea></td>;
        <td class='error'>$error_array[$label]</td></tr>";
    }
}
echo "<tr><td class='submit'><input type='hidden' name='submitted' value='yes'>
        <input type='submit' value='$submit'></td></tr>";
?>
</table>
</form>

這是驗證它的代碼。

<?php
$error_message = "";
$error_array = array();
if(isset($_POST['submitted']) and $_POST['submitted'] == "yes")
{
    foreach($_POST as $field => $value)
    {
        $name_patt = "/^[A-Za-z' -]{1,50}/";
        $phone_patt = "/^[0-9)(xX -]{7,20)$/";
        $addr_patt = "/^[A-Za-z0-9 .,'-]{1,50}$/";
        $email_patt = "/^.+@.+\\..+$/";
        if(preg_match("/first_name/i",$field))
        {
            if(!preg_match($name_patt,$value))
            {
                $error_array['first_name'] = "Please enter a valid first name.";
            }
        }
        if(preg_match("/last_name/i",$field))
        {
            if(!preg_match($name_patt,$value))
            {
                $error_array['last_name'] = "Please enter a valid last name.";
            }
        }
        if(preg_match("/email/i",$field))
        {
            if(!preg_match($email_patt,$value))
            {
                $error_array['email'] = "Please enter a valid email address.";
            }
        }
        if(preg_match("/subject/i",$field))
        {
            if(!preg_match($addr_patt,$value))
            {
                $error_array['subject'] = "Please enter a subject.";
            }
        }
        if(preg_match("/message/i",$field))
        {
            if(empty($_POST['message']))
            {
                $error_array['message'] = "Please enter a message.";
            }
        }
    }
    if(@sizeof($error_array >0))
    {
        include "contact.php";
        $error_message = "<p class='error'>One or more fields has been filled incorrectly.</p>";
        exit();
    }
    else
    {
        echo "Success!";
    }
}
else
{
    include "contact.php";
}

您可以使用AJAX將表單數據提交到PHP腳本。 在這種情況下,您可以將驗證或成功結果作為JSON並將其付諸實踐。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM