PHP学习笔记
2016-06-03 08:42
489 查看
表单
1. POST提交
<form method="post" action="send_simpleform.php"> //当提交后send_simpleform.php文件来处理提交数据,数据变量存放在全局变量$_POST中 <p><label for="user">Name:</label><br/> <input type="text" id="user" name="user"></p> <button type="sumbit" name="submit" value="send" >Send Message</button> //提交表单 </form>
1.1 获取表单输入数据
<p>Welcome,<?php echo $_POST['user']; ?></p>
提示:同样可以使用GET方法。POST可以处理比GET更多的数据,并且不会把数据传递到查询字符串中。如果使用了GET方法,确保将超全局变量修改为$_GET。
2. SELECT元素的HTML表单,列表选择元素
提示:SELECT列表一个或多个项目,html元素的name=名字+[],表示是数组,提交后数据存放在$_POST['products']的数组中。<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset="utf-8" /> <title>A simple HTML form</title> </head> <body> <form method="post" action="send_simpleform.php"> <p><label for="user">Name:</label><br/> <input type="text" id="user" name="user"></p> <fieldset> <legend>Select Some Products:</legend> <input type="checkbox" id="tricorder" name="products[]" value="Tricorder"> <label for="tricorder">Tricorder</label><br/> <input type="checkbox" id="ORAC_AI" name="products[]" value="ORAC_AT"> <label for="ORAC_AI">ORAC_AI</label><br/> <input type="checkbox" id="HAL_2000" name="products[]" value="HAL_2000"> <label for="HAL_2000">HAL_2000</label> </fieldset> <br/> <button type="submit" name="submit" value="send">Send Message</button> </form> </body> </html>
2.1 遍历选择结果
<!DOCTYPE html> <html> <head> </head> <body> <p>Name: <?php echo $_POST['user']; ?> </p> <p>Your product choices are: <?php if(!empty($_POST['products'])){ //检测当不为空时 echo "<ul>"; foreach($_POST['products'] as $value){ echo "<li>$value</li>"; } echo "</ul>"; } else{ echo "None"; } ?> </p> </body> </html>
3. HTML自身处理表单,即HTML和PHP代码混合在一个文件中
<!DOCTYPE html> <html> <head> <title>A PHP number guessing script</title> </head> <body> <?php $num_to_guess=42; if(!isset($_POST['guess'])){ //测试变量是否赋值 $message="Welcom to the guessing machine!"; }elseif(!is_numeric($_POST['guess'])){ //测试变量是否为数字 $message="I dot't understand that response."; }elseif($_POST['guess']==$num_to_guess){ $message="Well done!"; } ?> <h1><?php echo $message; ?> </h1> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <!--提交到自身,即重新加载当前页。--> <p><label for="guess">Type your guess here:</label><br/> <input type="text" id="guess" name="guess"/></p> <button type="submit" name="submit" value="submit">Submit</button> </form> </body> </html>
相关文章推荐
- php获取数据库中数据,转成json数据
- 仿制smarty模板
- ThinkPHP3.2.3实现分页的方法详解
- PHP学习笔记之php文件操作
- PHP中读取文件的几个方法总结(推荐)
- PHP读取大文件末尾N行的高效方法推荐
- PHP常用文件操作函数和简单实例分析
- 九个很有用的PHP代码
- 基于Thinkphp开发的微商城
- PPTP服务器配置选项详解
- PHP导入Excel和导出数据为Excel文件
- php excel类 phpExcel使用方法介绍
- PHP之图像处理
- wamp通过phpMyAdmin修改登录密码
- php数组
- 将php和mysql命令加入到环境变量中
- php二分查找法
- php过滤ascii控制字符
- php连接数据库的相关操作
- php 设计模式之简单工厂模式