PHP课程 - 第1课 - 表单操作
在日常生活中,我们通过电视、广播、互联网或面对面的交流来获取信息。网站也需要从用户那里获取信息,但它们通过一种特殊的方式来实现——表单。表单可以类比为问卷调查或官方申请表。虽然表单的元素类型有限,但它们能让网站收集到所有必要的信息以完成交互。
为了更好地理解本课内容,建议你先学习 HTML 表单的创建方法。
在开始第一个 PHP 课程之前,我们先创建一个程序框架,也就是一个用于控制网站运行的类。
创建一个名为 class 的文件夹,并在其中放置一个文件 simpleCMS.php,它将包含我们的网站控制类。同时创建一个 index.php 文件用于启动该系统。我们还需要创建一个用于保存消息的空文件夹 messages,以及样式文件 style.css。结构如下:
class/simpleCMS.php
messages/
index.php
style.css
simpleCMS.php 文件内容如下:
<?php class simpleCMS { // 控制类 public function display_public() { // 显示消息的方法 } public function display_admin() { // 显示输入表单的方法 } public function write($p) { // 写入消息的方法 } } ?>
我们暂时让这些方法保持空白,稍后再补充代码。
style.css 文件:
*{ margin: 0; padding: 0; } body{ font: 12px "Lucida Grande", Sans-Serif; background: #ccc; } #page-wrap{ width: 500px; margin: 50px auto; padding: 20px; background: white; } h1, h2, h3{ font: 28px Georgia, Serif; border-bottom: 1px dotted #ccc; margin: 0 0 10px 0; } .clear{ clear: both; } input[type="text"], textarea{ padding: 3px; border: 1px solid #666; width: 350px; margin: 0 0 15px 0; } input[type="text"]{ font: 28px Georgia, Serif; } textarea { height: 100px; font: 12px "Lucida Grande", Sans-Serif; } label{ background: #666; color: white; padding: 2px 6px; } .post{ margin: 0 0 20px 0; }
index.php 文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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>学习 PHP</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="page-wrap"> <?php include_once('class/simpleCMS.php'); // 引入类文件 $obj = new simpleCMS(); // 创建控制类对象 ?> </div> </body> </html>
当网站加载时,浏览器首先执行 index.php 文件。目前页面看起来是空的,但我们已经包含了类文件并创建了对象。稍后我们将调用对象的方法来实现网站功能,而这些调用会通过条件语句 if 来判断。
在继续编写文件之前,让我们先了解一下两个超级全局变量:$_GET 和 $_POST。
超级全局变量 $_GET
HTML 表单有一个属性 method(可以是 get 或 post),决定了表单数据提交后会被放入哪个全局变量中。举个例子:
<form action="welcome.php" method="get"> 姓名: <input type="text" name="fname" /> 年龄: <input type="text" name="age" /> <input type="submit" /> </form>
点击提交后,浏览器会跳转到:
http://sitename/welcome.php?fname=Peter&age=37
此时我们可以在 PHP 中这样读取数据:
<?php print $_GET['fname']; print $_GET['age']; ?>
$_GET 是一个数组,存储了 URL 中问号 ? 之后的参数。我们甚至可以不使用表单,而直接通过 URL 传递数据:
http://sitename/example.php?number=45
执行如下代码:
<?php print $_GET['number']; ?>
页面将输出 URL 中的数字。
超级全局变量 $_POST
$_POST 的使用方式与 $_GET 类似,但数据不会出现在 URL 中。我们来看一个例子:
<form action="welcome.php" method="post"> 姓名: <input type="text" name="fname" /> 年龄: <input type="text" name="age" /> <input type="submit" /> </form>
提交后跳转到:
http://sitename/welcome.php
在 PHP 中读取数据:
<?php print $_POST['fname']; print $_POST['age']; ?>
与 $_GET 不同,$_POST 的内容不会显示在地址栏中,只能在 PHP 中访问。
创建消息提交表单
我们将使用 POST 方法提交消息,而网站的管理逻辑通过 GET 参数来控制。表单代码如下:
public function display_admin() { // 输入消息的方法 $content = ''; $content .= '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">'; // $_SERVER['PHP_SELF'] 返回当前执行的文件名 $content .= '<label for="title">姓名:</label><br />'; $content .= '<input name="title" id="title" type="text" maxlength="150" />'; $content .= '<div class="clear"></div>'; $content .= '<label for="bodytext">消息:</label><br />'; $content .= '<textarea name="bodytext" id="bodytext"></textarea>'; $content .= '<div class="clear"></div>'; $content .= '<input type="submit" value="添加消息" />'; $content .= '</form>'; $content .= '<p><a href="/index.php">返回首页</a></p>'; return $content; }
将上面的代码替换进 display_admin() 方法中。此表单将在添加新消息时显示。默认情况下我们应该显示消息列表。我们约定:当 URL 中包含参数 admin=1 时显示表单。例如:
http://test/index.php?admin=1
修改 index.php 文件如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" lang="ru" dir="ltr"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>学习 PHP</title> <link rel="stylesheet" type="text/css" href="style.css" /> </head> <body> <div id="page-wrap"> <?php include_once('class/simpleCMS.php'); // 引入类文件 $obj = new simpleCMS(); // 创建对象 if( $_GET['admin'] == 1 ){ // 如果 URL 中有 admin 参数 print $obj->display_admin(); // 显示表单 }else{ print $obj->display_public(); // 否则显示消息 } ?> </div> </body> </html>
更新 display_public() 方法,用于在主页显示添加链接:
public function display_public() { // 输出消息的方法 $content = ''; $content .= '<p><a href="/index.php?admin=1">添加消息</a></p>'; return $content; }
如果加载页面时出现乱码,可以在网站根目录创建一个名为 .htaccess 的文件,并添加以下行:
AddDefaultCharset utf-8
这样服务器会以 UTF-8 编码工作。
现在我们已经完成了一个可以提交数据的表单。为了验证是否正确,可以临时将表单的 method 改为 get:
$content .= '<form action="' . $_SERVER['PHP_SELF'] . '" method="get">';
现在表单数据会出现在 index.php 的 URL 中。我们将在下一课学习如何处理这些数据,并把它们保存到文件中。