php基础教程——2创建模板、处理表单
2015-01-16 17:13
701 查看
一.创建模板:
将页面中经常出现的部分复制到一个html或php文件中,在原页面中用require()/include()函数引入。
例子:
源html:
复制的头部:header.html:
复制的尾部:footer.html:
合成模板:ws.php
二、处理表单——让一个页面同时显示和处理表单
使用条件语句:
if (表单提交) { 处理表单 }
else { 显示表单 }
例子:简单的 用户名-密码验证
输入:用户名:YF 密码:123456
显示:登陆成功
裁图:
代码:ws.php(其中header.html、footer.html引用的是上文中的模板):
将页面中经常出现的部分复制到一个html或php文件中,在原页面中用require()/include()函数引入。
例子:
源html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title><base> <body> <form action="handle.php" method="post"> <p> Name:<select name="title"> <option value="Mr">Mr</option> </select> <input type="text" name="name" size="20"/></p> <input type="submit" value="send"> </form> <div><p>This is the foot of the document</p></div> </body> </html>
复制的头部:header.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>test</title><base> <body>
复制的尾部:footer.html:
<div><p>This is the foot of the document</p></div> </body> </html>
合成模板:ws.php
<?php require('header.html'); ?> <form action="handle.php" method="post"> <p> Name:<select name="title"> <option value="Mr">Mr</option> </select> <input type="text" name="name" size="20"/></p> <input type="submit" value="send"> </form> <?php require('footer.html'); ?>
二、处理表单——让一个页面同时显示和处理表单
使用条件语句:
if (表单提交) { 处理表单 }
else { 显示表单 }
例子:简单的 用户名-密码验证
输入:用户名:YF 密码:123456
显示:登陆成功
裁图:
代码:ws.php(其中header.html、footer.html引用的是上文中的模板):
<?php define('TITLE', 'Login'); require('header.html'); if (isset($_POST['submitted'])) { if ((!empty($_POST['name'])) && (!empty($_POST['password']))){ if ((strtolower($_POST['name']) == 'yf') && ($_POST['password'] == '123456')){// name and password are correct. print '<p> logged in !</p>'; } else { print '<p> name or password is worry!</p>'; } } else { print '<p> make sure you enter both name and password!</p>'; } } else { print '<form action="ws.php" method="post"> <p> Name:<input type="text" name="name" size="20"/></p> <p>Password:<input type="password" name="password" "size="20" /></p> <input type="submit" value="send"> <input type="hidden" name="submitted" value="true"/> </form>'; } require('footer.html'); ?>
相关文章推荐
- [李景山php]每天laravel[022]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---Task 任务处理控制器--显示主页
- [李景山php]每天laravel[021]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---Task 任务处理控制器
- PHP基础----HTML表单的创建与提交----18表单与 PHP
- PHP基础----HTML表单的创建与提交----19处理 GET 请求
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---删除数据
- php入门之表单创建和基本处理
- [李景山php]每天laravel[017]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---准备数据库
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---登录,该死的登录
- [李景山php]每天laravel[019]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---路由
- php基础教程——表单验证(必填、提交后数据保留)
- PHP基础教程 PHP的页面缓冲处理机制
- PHP基础----HTML表单的创建与提交----20处理 POST 请求
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---主页,亲爱的,我回来了!!
- [转]php初级教程(五)php表单处理
- InfoPath基础应用教程-1 设计一个简单的表单模板
- php动态图像处理--php基础最详细教程
- 7天入门php-基础表单处理
- PHP基础教程-11 创建单位转换器
- [李景山php]每天laravel[023]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---在主页上添加数据
- [李景山php]每天laravel[018]-laravel 中级任务--小白教程----实际操作-创建基础任务 to-do-list 案例 ---准备关系模型