search
Homephp教程php手册php $_POST[]获取表单数据方法

在php获取表单数据有专用的命令,$_POST[]就是这个函数了,下面来看我们一个简单的实例:

<?php 
	echo &#39;Hello, &#39; . $_POST[&#39;first_name&#39;] . &#39;!&#39;; 
	 

输出是来自表单的first_name的值,再看一个复杂一点点的实例:

$_POST['name'] = trim($_POST['name']); 
	 
	if (strlen($_POST['name']) == 0) { 
	    $errors[] = "Your name is required."; 
	} 

好了,下面做一个关于$_POST的完整实例:

 class="brush:php;"><?php  
	  $user =  $_POST[&#39;user&#39;]; 
	  $color = $_POST[&#39;color&#39;]; 
	  $self =  $_SERVER[&#39;PHP_SELF&#39;]; 
	 
	  if( ( $user != null ) and ( $color != null ) ) 
	  { 
	    setcookie( "firstname", $user , time() + 36000 ); 
	    setcookie( "fontcolor", $color, time() + 36000 ); 
	    header( "Location:getcookie.php" ); 
	    exit(); 
	  } 
	 
	<html> 
	 <head> 
	  <title>Set Cookie Data</title> 
	 </head> 
	 <body> 
	 
	  <form action ="<?php echo( $self ); " method = "post"> 
	 
	  Please enter your first name: 
	  <input type = "text" name = "user"><br><br> 
	 
	  Please choose your favorite font color:<br> 
	  <input type = "radio" name = "color" value = "#FF0000">Red 
	  <input type = "radio" name = "color" value = "#00FF00">Green 
	  <input type = "radio" name = "color" value = "#0000FF">Blue 
	  <br><br> 
	  <input type = "submit" value = "submit"> 
	  </form> 
	 
	 </body> 
	 
	</html> 

总结:上面的实例没做安全处理,只是简单的获取了表单提交的数据,我们可以做一些安全处理,如isset() addslashes 等处理.

本文地址:

转载随意,但请附上文章地址:-)

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools