Home > php教程 > php手册 > php 获取表单数据的实现方法

php 获取表单数据的实现方法

WBOY
Release: 2016-05-25 16:46:29
Original
1623 people have browsed it

这是一篇基础的php入门教程,我们要告诉你如何利用php的相关函数来获取表单数据,在php提供了post,get,request三种方法来获取数据,我们最常用的就是$_POST,$_GET方法了,看$_REQUEST 变量,PHP 的 $_REQUEST 变量包含了 $_GET,$_POST 以及 $_COOKIE 的内容.

PHP 的 $_REQUEST 变量可用来取得通过 GET 和 POST 方法发送的表单数据的结果.

实例一:

 class="brush:php;"><html> 
	<head> 
	<title>An HTML Form that Calls Itself</title> 
	</head> 
	<body> 
	<div> 
	<?php 
	if ( ! emptyempty( $_POST[&#39;guess&#39;] ) ) { 
	    print "last guess: ".$_POST[&#39;guess&#39;]; 
	} 
	 
	<form method="post" action="<?php print $_SERVER[&#39;PHP_SELF&#39;]"> 
	<p> 
	Type your guess here: <input type="text" name="guess" /> 
	</p> 
	</form> 
	</div> 
	</body> 
	</html> 
Copy after login

由于表的提供大量数据时我们尽量用post来获取,更复杂的实例二

实例二:

<?php 
	 
	$self = $_SERVER[&#39;PHP_SELF&#39;]; 
	$username = $_POST[&#39;username&#39;]; 
	$useraddr = $_POST[&#39;useraddr&#39;]; 
	$comments = $_POST[&#39;comments&#39;]; 
	$sent = $_POST[&#39;sent&#39;]; 
	 
	$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 = "/^[-!#$%&&#39;*+./0-9=?A-Z^_&#39;{|}~]+"; 
	  $_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");} 
	 
	} 
	 
Copy after login

上面可对用户提交的数据进行一些相关处理了,上只讲了$_POST

$_POST 变量

$_POST 变量是一个数组,内容是由 HTTP POST 方法发送的变量名称和值.

$_POST 变量用于收集来自 method="post" 的表单中的值,从带有 POST 方法的表单发送的信息,对任何人都是不可见的(不会显示在浏览器的地址栏),并且对发送信息的量也没有限制.

永久链接:

转载随意!带上文章地址吧。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template