这是一篇基础的php入门教程,我们要告诉你如何利用php的相关函数来获取表单数据,在php提供了post,get,request三种方法来获取数据,我们最常用的就是$_POST,$_GET方法了,看$_REQUEST 变量,PHP 的 $_REQUEST 变量包含了 $_GET,$_POST 以及 $_COOKIE 的内容.
PHP 的 $_REQUEST 变量可用来取得通过 GET 和 POST 方法发送的表单数据的结果.
实例一:
- <html>
- <head>
- <title>An HTML Form that Calls Itself</title>
- </head>
- <body>
- <div>
- <?php
- if ( ! emptyempty( $_POST['guess'] ) ) {
- print "last guess: ".$_POST['guess'];
- }
- ?>
- <form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
- <p>
- Type your guess here: <input type="text" name="guess" />
- </p>
- </form>
- </div>
- </body>
- </html>
由于表的提供大量数据时我们尽量用post来获取,更复杂的实例二
实例二:
- <?php
-
- $self = $_SERVER['PHP_SELF'];
- $username = $_POST['username'];
- $useraddr = $_POST['useraddr'];
- $comments = $_POST['comments'];
- $sent = $_POST['sent'];
-
- $form ="<form action="$self" method="post">";
- $form.="Name:<input type="text" name="username"";
- $form.=" size="30" value="$username" >";
- $form.="Email:<input type="text" name="useraddr"";
- $form.=" size="30" value="$useraddr">";
- $form.="Comments:<textarea name="comments" >";
- $form.="$comments</textarea><br/>";
- $form.="<input type="submit" name="sent" value="Send Form">";
- $form.="</form>";
-
- if($sent)
- {
- $valid=true;
-
- if( !$username )
- { $errmsg.="Enter your name...<br />"; $valid = false; }
-
- if( !$useraddr )
- { $errmsg .="Enter your email address...<br />"; $valid = false; }
-
- if( !$comments )
- { $errmsg.="Enter your comments...<br />"; $valid = false; }
-
- $useraddr = trim($useraddr);
- $_name = "/^[-!#$%&'*+./0-9=?A-Z^_'{|}~]+";
- $_host = "([-0-9A-Z]+.)+";
- $_tlds = "([0-9A-Z]){2,4}$/i";
- if( !preg_match( $_name."@".$_host .$_tlds,$useraddr ) )
- {
- $errmsg.="Email address has incorrect format!<br />";
- $valid=false;
- }
- }
-
- if($valid != true)
- {
- echo( $errmsg.$form );
- }
- else
- {
- $to = "php@h.com";
-
- $re = "Feedback from $username";
-
- $msg = $comments;
-
- $headers = "MIME-Version: 1.0rn";
- $headers .= "Content-type: text/html;";
- $headers .= "charset="iso-8859-1"rn";
-
- $headers .= "From: $useraddr rn";
-
- if(mail($to,$re,$msg, $headers))
- { echo("Your comments have been sent - thanks $username");}
-
- }
- ?>
上面可对用户提交的数据进行一些相关处理了,上只讲了$_POST
$_POST 变量
$_POST 变量是一个数组,内容是由 HTTP POST 方法发送的变量名称和值.
$_POST 变量用于收集来自 method="post" 的表单中的值,从带有 POST 方法的表单发送的信息,对任何人都是不可见的(不会显示在浏览器的地址栏),并且对发送信息的量也没有限制.