PHP表單處理之空白檢測

文件form_phone_values.inc中@$$field的意思:
$field是一個變量,在遍歷$labels時它可以是first_name,middle_name,last_name或phone,對應(yīng)地$$label就會變成變量$first_name,$middle_name,$last_name或$phone,這用于在輸入過一次表單后提交失敗的情況下在表單中依然保留用戶的輸入。
@符號表示如果是第一次加載這個頁面,$first_name,$middle_name,$last_name和$phone并不存在,此時不提示錯誤信息。

<?php
/*  Program name: form_phone_values.inc
*  Description:  Defines a form that collects a user's
*                name and phone number.
*/
$labels = array( "first_name" => "First Name",
                 "middle_name" => "Middle Name",
                 "last_name" => "Last Name",
                 "phone" => "手機號碼");
$submit = "提交";
?>
<html>
<head><meta charset="utf-8"><title>Customer Phone Number</title>
<style type='text/css'>
<!--
#form {
    margin: 1.5em 0 0 0;
    padding: 0;
}
#field {padding-bottom: 1em;}
label {
    font-weight: bold;
    float: left;
    width: 20%;
    margin-right: 1em;
    text-align: right;
}
-->
</style>
</head>
<body>
<h3>請在下面輸入您的手機號碼</h3>
<?php
//$_SERVER[PHP_SELF]指"當(dāng)前腳本的路徑/文件名"
echo "<form action='$_SERVER[PHP_SELF]' method='POST'>
<div id='form'>";
if(isset($message))
{
    echo $message;
}
/* Loop that displays the form fields */
foreach($labels as $field => $label)
{
    echo "<div id='field'><label for='$field'>$label</label>
    <input id='$field' name='$field' type='text'
    size='50%' maxlength='65'
    value='".@$$field."' /></div>\n";
    }
    echo "<input type='hidden' name='sent' value='yes' />\n";
    echo "<input style='margin-left: 33%' type='submit'
    value='$submit' />\n";
?>
</form></body></html>

strip_tags() 函數(shù)剝?nèi)プ址械?HTML、XML 以及 PHP 的標簽。
PHP extract() 函數(shù)從數(shù)組中把變量導(dǎo)入到當(dāng)前的符號表中。對于數(shù)組中的每個元素,鍵名用于變量名,鍵值用于變量值。第二個參數(shù) type 用于指定當(dāng)某個變量已經(jīng)存在,而數(shù)組中又有同名元素時,extract() 函數(shù)如何對待這樣的沖突。本函數(shù)返回成功設(shè)置的變量數(shù)目。

<?php
/*  Program name: checkBlank.php
*  Description:  Program checks all the form fields for
*                blank fields.
*/
if(isset($_POST['sent']) && $_POST['sent'] == "yes")
{
    /* check each field except middle name for blank fields */
    foreach($_POST as $field => $value)
    {
        if($value == "")
        {
            if($field != "middle_name")
            {
                $blank_array[] = $field;
            } // endif field is not middle name
        } // endif field is blank
        else
        {
            $good_data[$field] = strip_tags(trim($value));
        }
    }  // end of foreach loop for $_POST
    /* if any fields were blank, create error message and redisplay form */
    if(@sizeof($blank_array) > 0)
    {
        $message = "<p style='color: red; margin-bottom: 0; font-weight: bold'>
    You didn't fill in one or more required fields. You must enter:
    <ul style='color: red; margin-top: 0; list-style: none' >";
    
        /* display list of missing information */
        foreach($blank_array as $value)
        {
            $message .= "<li>$value</li>";
        }
        $message .= "</ul>";
    
        /* redisplay form */
        extract($good_data);
        include("form_phone_values.inc");
        exit();
    } // endif blanks
    echo "All required fields contain information";
} // endif submitted
else
{
    include("form_phone_values.inc");
}
?>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容