php $_POST[]获取表单数据方法
来源:程序员人生 发布时间:2014-05-04 02:56:51 阅读次数:2204次
在php获取表单数据有专用的命令,$_POST[]就是这个函数了,下面来看我们一个简单的实例:
- <?php
- echo 'Hello, ' . $_POST['first_name'] . '!';
- ?>
输出是来自表单的first_name的值,再看一个复杂一点点的实例:
- $_POST['name'] = trim($_POST['name']);
-
- if (strlen($_POST['name']) == 0) {
- $errors[] = "Your name is required.";
- }
好了,下面做一个关于$_POST的完整实例:
- <?php
- $user = $_POST['user'];
- $color = $_POST['color'];
- $self = $_SERVER['PHP_SELF'];
-
- 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 等处理.
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠