文件form_phone_values.inc中@$$field的意思:
$field是一個變量把跨,在遍歷$labels時(shí)它可以是first_name,middle_name,last_name或phone慷暂,對應(yīng)地$$label就會變成變量$first_name,$middle_name,$last_name或$phone辕宏,這用于在輸入過一次表單后提交失敗的情況下在表單中依然保留用戶的輸入力图。
@符號表示如果是第一次加載這個頁面势腮,$first_name,$middle_name,$last_name和$phone并不存在饲漾,此時(shí)不提示錯誤信息珍剑。
<?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" => "手機(jī)號碼");
$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>請?jiān)谙旅孑斎肽氖謾C(jī)號碼</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 的標(biāo)簽穆壕。
PHP extract() 函數(shù)從數(shù)組中把變量導(dǎo)入到當(dāng)前的符號表中。對于數(shù)組中的每個元素严嗜,鍵名用于變量名粱檀,鍵值用于變量值。第二個參數(shù) type 用于指定當(dāng)某個變量已經(jīng)存在漫玄,而數(shù)組中又有同名元素時(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");
}
?>