簡體   English   中英

從表單(PHP)中提取輸入的數據

[英]Extract Entered Data from Form (PHP)

我目前正在將課程作為我的Web設計程序的一部分,而我的最終項目需要創建一個有效的注冊表格。 雖然表單可以正確驗證數據,但是一旦用戶提交數據,我就無法保存輸入的任何數據。 本課程已指示我使用“提取”功能來重新填充表單輸入,但是在過去,使用此功能也存在很多問題。 以下是我的表單的代碼:

<?php
$labels = array("first_name" => "First Name*:", 
                "last_name" => "Last Name*:",
                "company" => "Company*:",
                "address" => "Address*:",
                "city" => "City*:",
                "province_state" => "Province/State*:",
                "postal_zip" => "Postal/Zip Code*:",
                "phone" => "Telephone:",
                "fax" => "Fax:",
                "email" => "Email Address*:",
                "comments" => "Comments:");
$submit = "Submit";
?>

...

<form method="post">
<table class='form'>
<?php
foreach($labels as $label => $field)
{
    if($label == 'postal_zip')
    {
        echo "<tr><td class='label'><label for='$label'>$field</label></td>
        <td class='input'><input type='text' name='$label' maxlength='9'></td></tr>";
    }
    if($label == 'phone' or $label == 'fax')
    {
        echo "<tr><td class='label'><label for='$label'>$field</label></td>
        <td class='input'><input type='text' name='$label' maxlength='14'></td></tr>";
    }
    if($label == 'comments')
    {
        echo "<tr><td class='label'><label for='$label'>$field</label></td>
        <td class='input'><textarea name='$label' cols='30' rows='5'></textarea></td></tr>";
    }
    else
    {
        if($label != 'postal_zip' && $label != 'phone' && $label != 'fax' && 'comments')
        {
            echo "<tr><td class='label'><label for='$label'>$field</label></td>
            <td class='input'><input type='text' name='$label' value='$value'></td></tr>";
        }
    }
    echo "</tr>";
}
echo "<tr><td></td>
        <td><input type='hidden' name='submitted' value='yes'>
        <input type='submit' name='$submit' value='$submit'></td>";
?>
</table>
</form>

以下是我的驗證代碼。

<?php
$message = "";
if(isset($_POST['submitted']) and $_POST['submitted'] == 'yes')
{
    foreach($_POST as $field => $value)
    {
        if(empty($value) or !empty($value))
        {
            $email_patt = "/^.+@.+\\..+$/";
            $name_patt = "/^[A-Za-z' -]{1,50}/";
            $addr_patt = "/^[A-Za-z0-9 .,'-]{1,50}$/";
            $postal_patt = "/^[A-Za-z0-9-]{1,10}$/";
            $phone_patt =  "/^[0-9)(xX -]{7,20}$/";
            if(preg_match("/first_name/i",$field))
            {
                if(!preg_match($name_patt,$value))
                {
                    $error_array[] = "Please enter a valid first name.";
                }
            }
            if(preg_match("/last_name/i",$field))
            {
                if(!preg_match($name_patt,$value))
                {
                    $error_array[] = "Please enter a valid last name.";
                }
            }
            if(preg_match("/address/i",$field))
            {
                if(!preg_match($addr_patt,$value))
                {
                    $error_array[] = "Please enter a valid address.";
                }
            }
            if(preg_match("/company/i",$field))
            {
                if(!preg_match($addr_patt,$value))
                {
                    $error_array[] = "Please enter a valid company name.";
                }
            }
            if(preg_match("/city/i",$field))
            {
                if(!preg_match($addr_patt,$value))
                {
                    $error_array[] = "Please enter a valid city.";
                }
            }
            if(preg_match("/country/i",$field))
            {
                if(!preg_match($addr_patt,$value))
                {
                    $error_array[] = "Please enter a valid address.";
                }
            }
            if(preg_match("/postal_zip/i",$field))
            {
                if(!preg_match($postal_patt,$value))
                {
                    $error_array[] = "Please enter a valid postal/zip code.";
                }
            }
            if(preg_match("/email/i",$field))
            {
                if(!preg_match($email_patt,$value))
                {
                    $error_array[] = "Please enter a valid email address.";
                }
            }
        }
        $good_data[$field] = strip_tags(trim($value));
    }
    if(@sizeof($error_array) >0)
    {
        $message = "<ul class='error'>";
        foreach($error_array as $value)
        {
            $message .= "<li>$value</li>";
        }
        $message .= "</ul>";
        extract($good_data);
        include "contact_form.php";
        exit();
    }
    else 
    {
        header('location: success.html');
    }
}
else
{
    include "contact_form.php";
}
?>

目前,我已將所有$ _POST值放入數組$ good_data中,並在驗證代碼中使用print_r,獲得了用戶輸入的所有字段和值的列表(盡管它確實包括了頁面上的所有輸入) )。 是否有一種簡單的方法將這些值放在每個字段中?

感謝Dagon對我的問題的回答。 通過將$ _POST [$ field]添加到每個字段的值,它可以保留數據並正常運行。

暫無
暫無

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

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