其实书是差不多看完了,但这个还没写完。暂时不用PHP。先这样了。
第二章 PHP编程
2.1 创建HTML表单
Steps:
①创建HTML表单自身。
②创建相应的PHP脚本,用于接受和处理表单数据。
HTML表单是使用form标签和多种用于获取输出的元素创建的。
<form action="script.php" method="post">
</form>
对PHP来讲,form标签制定吧表单数据发送到哪个页面。
第二个参数,选择方法指定如何把数据发送到处理页面。get和post指示要使用的HTTP方法。
get方法把提交的数据通过一系列追加到URL后面的名-值对发送到接收页面。好处是可以在用户的WEB浏览器中为得到的页面建立书签。还可以后退返回到一个get页面,或者重新加载它。而post不能。但这种方法传输数据量有限,而且因为数据可见而不安全。
一般,get用于请求信息,如数据库中特定记录或者搜索的结果。需要采取一个动作时,如在更新数据库记录或者发送Email时,就用post。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Simple HTML Form</title>
<!-- 设置CSS样式 ,粗体,蓝色-->
<style type="text/css" title="text/css" media="all">
label {
font-weight: bold;
color: #300ACC;
}
</style>
</head>
<body>
<!-- Script 2.1 - form.html -->
<!--添加初始form标签 -->
<form action="handle_form.php" method="post">
<!-- 开始创建HTML表单。<fieldset><legend>会在表单周围加一个框。 -->
<fieldset><legend>Enter your information in the form below:</legend>
<!-- 添加文本输入框。 -->
<p><label>Name: <input type="text" name="name" size="20" maxlength="40" /></label></p>
<p><label>Email Address: <input type="text" name="email" size="40" maxlength="60" /></label></p>
<!-- 添加单选按钮 -->
<p><label for="gender">Gender: </label><input type="radio" name="gender" value="M" /> Male <input type="radio" name="gender" value="F" /> Female</p>
<!-- 添加下拉菜单。 -->
<p><label>Age:
<select name="age">
<option value="0-29">Under 30</option>
<option value="30-60">Between 30 and 60</option>
<option value="60+">Over 60</option>
</select></label></p>
<!-- 添加文本框。 -->
<p><label>Comments: <textarea name="comments" rows="3" cols="40"></textarea></label></p>
</fieldset>
<!-- 创建提交按钮。 -->
<p align="center"><input type="submit" name="submit" value="Submit My Information" /></p>
</form>
</body>
</html>
也可为表单制定一种默认字符编码。默认情况下,页面会使用自身的字符编码来提交数据。
<form accept-charset="utf-8">
运行效果:
2.2 编写一个新的PHP脚本处理HTML表单
PHP能够与HTML表单极好地进行交互。PHP脚本吧接收到的信息存储在特殊的变量中。
如表单中有一个输入:
<input type="text" name="city" />
可通过一个名为$_REQUEST[ ‘city' ]的PHP变量访问它。$REQUEST是一个特殊的变量类型,叫超全局变量。村出了通过Get或者Post发送到PHP页面的所有数据,以及在Cookie可访问的数据。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Form Feedback</title>
</head>
<body>
<?php # Script 2.2 - handle_form.php
// Create a shorthand for the form data:
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$comments = $_REQUEST['comments'];
// Print the submitted information:
echo "<p>Thank you, <b>$name</b>, for the following comments:<br />
<tt>$comments</tt></p>
<p>We will reply to you at <i>$email</i>.</p>\n";
?>
</body>
</html>
将该文件放在与form.html同一目录下。
填写数据,运行效果如下。
Thank you, HE, for the following comments:
DSS
We will reply to you at EEW.
Tip:如果在打印表单数据时看见\,,可用stripslashes($var)去掉。
2.3 条件语句和运算符
需注意isset($var),如果$var具有非NULL的任何值,非0值、FALSE或者空字符串,即变量已被设置,条件为真。
一些逻辑运算符。
&& AND
|| OR
XOR
2.4 验证表单数据
应确保有输入,并且数据有正确的类型和格式或特定的可接受值。
要检查用户是否有输入文本,应用empty()。
测试要提交的值是否数字,可用is_numeric()。
2.5 数组
PHP支持索引数组(indexed array)和关联数组(associate array)。
打印关联数组的时候,有时括住key的括号会使语法混乱,引发解析错误。可用花括号括住数组名。
echo "IL is {$states[ 'il' ]}.";
Reference 超全局数组
比如$_GET、$_POST、$_REQUEST、 $_SERVER、 $_ENV、 $_SESSION 和$_COOKIE。注意精确使用变量,以具有良好的安全性。
2.5.1 创建数组
①一次添加一个元素。
$band[] = 'J'
$band[] = 'ew'
②添加元素时指定key。
$band['eew'] = 'mei';
③用array()一次添加多个元素。
$states = array('IA' =>‘IWOE', 'MD'=> 'ewd');
$artists = array('James','Wiss');
$days = array(1=>'Sun','Mon');
echo $days[2];
$tv = array();
$tv[] = 'Fight!';
$ten = range(1,10);//用range()快速构建数字数组
2.5.2 访问数组
访问每个数组元素,可用foreach循环。
foreach ($array as $value)
{
}
foreach ($array as $key=>$value)
{
}
Tip:
①可用count()确定数组中元素个数。
②range()还可以创建连续字母数组。
③可用is_array()确认一个变量是数组类型。
$num = count($array);
$alp = range('a','z');
2.5.3 多维数组
// Create one array:
$mexico = array(
'YU' => 'Yucatan',
'BC' => 'Baja California',
'OA' => 'Oaxaca'
);
// Create another array:
$us = array (
'MD' => 'Maryland',
'IL' => 'Illinois',
'PA' => 'Pennsylvania',
'IA' => 'Iowa'
);
// Create a third array:
$canada = array (
'QC' => 'Quebec',
'AB' => 'Alberta',
'NT' => 'Northwest Territories',
'YT' => 'Yukon',
'PE' => 'Prince Edward Island'
);
// Combine the arrays:
$n_america = array(
'Mexico' => $mexico,
'United States' => $us,
'Canada' => $canada
);
// Loop through the countries:
foreach ($n_america as $country => $list) {
// Print a heading:<ul>打印初始无序列表标签
echo "<h2>$country</h2><ul>";
// Print each state, province, or territory:
foreach ($list as $k => $v) {
echo "<li>$k - $v</li>\n";
}
// Close the list:
echo '</ul>';
} // End of main FOREACH.