PHP怎么获取表单提交的数据啊?

<?php
$name = $_POST[name];
echo $name;
?>
<html>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text" name="name" />
<input type="submit" value="提交" />
</form>
</body>
</html>
我把这个提交的name里面的东西提交给本页,应该能打印出来我输入的东西啊,怎么现在报错?
$name = $_POST[‘name’];
这句加了引号还是不行啊
Notice: Undefined index: name in D:\Program Files\EasyPHP 3.0\www\aaa.php on line 2

一、用file_get_contents以get方式获取内容,需要输入内容为:

1、<?php

2、$url='http://www.domain.com/?para=123';

3、$html = file_get_contents($url);

4、echo $html;

5、?>

二、用file_get_contents函数,以post方式获取url,需要输入内容为

1、<?php

2、$url = 'http://www.domain.com/test.php?id=123';

3、$data = array ('foo' => 'bar');

4、$data = http_build_query($data);

5、$opts = array (

6、'http' => array (

7、   'method' => 'POST',

8、  'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .

9、                     "Content-Length: " . strlen($data) . "\r\n",

10、   'content' => $data

11、)

12、);

13、$ctx = stream_context_create($opts);

14、$html = @file_get_contents($url,'',$ctx);

15、?>

三、用fopen打开url,以get方式获取内容,需要输入内容为

1、<?php

2、$fp = fopen($url, 'r');

3、$header = stream_get_meta_data($fp);//获取信息

4、while(!feof($fp)) {

5、$result .= fgets($fp, 1024);

6、}

7、echo "url header: {$header} <br>":

8、echo "url body: $result";

9、fclose($fp);

10、?>

四、用fopen打开url,以post方式获取内容,需要输入内容为

1、<?php

2、$data = array ('foo2' => 'bar2','foo3'=>'bar3');

3、$data = http_build_query($data);

4、$opts = array (

5、'http' => array (

6、'method' => 'POST',

7、'header'=> "Content-type: application/x-www-form-urlencoded\r\nCookie:cook1=c3;cook2=c4\r\n" .

8、"Content-Length: " . strlen($data) . "\r\n",

9、'content' => $data

10、)

11、);

12、$context = stream_context_create($opts);

13、$html = fopen('http://www.test.com/zzzz.php?id=i3&id2=i4','rb' ,false, $context);

14、$w=fread($html,1024);

15、echo $w;

16、?>

五、用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,需要输入内容为

1、?php

2、function get_url ($url,$cookie=false)

3、{

4、$url = parse_url($url);

5、$query = $url[path]."?".$url[query];

6、echo "Query:".$query;

7、$fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);

8、if (!$fp) {

9、return false;

10、} else {

11、$request = "GET $query HTTP/1.1\r\n";

12、$request .= "Host: $url[host]\r\n";

13、$request .= "Connection: Close\r\n";

14、if($cookie) $request.="Cookie:   $cookie\n";

15、$request.="\r\n";

16、fwrite($fp,$request);

17、while(!@feof($fp)) {

18、$result .= @fgets($fp, 1024);

19、}

20、fclose($fp);

21、return $result;

22、}

23、}

24、//获取url的html部分,去掉header

25、function GetUrlHTML($url,$cookie=false)

26、{

27、$rowdata = get_url($url,$cookie);

28、if($rowdata)

29、{

30、$body= stristr($rowdata,"\r\n\r\n");

31、$body=substr($body,4,strlen($body));

32、return $body;

33、}

34、   return false;

35、}

36、?>

参考资料:

php-file_get_contents

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-09-01
PHP 可以通过POST、GET方法获取到表单提交的数据
获取到的POST、GET是数组形式的值,需要通过键值来详细获取相应的值

比如: index.php 页面
下面是POST方法
<form name="form1" method="post" action="index.php">
<input type="text" name="contents" value="">
<input type="submit" value="提交">
</form>
<?php
//获取表单提交的数据
$contents = $_POST['contents'];

echo $contents;
?>
也可以是下面是GET方法
<form name="form1" method="get" action="index.php">
<input type="text" name="contents" value="">
<input type="submit" value="提交">
</form>
<?php
//获取表单提交的数据
$contents = $_GET['contents'];

echo $contents;
?>

POST相对于GET方法,更好一些,可以提交大量数据,以及更安全些。
第2个回答  推荐于2017-09-10
因为再没提交前,取不到name。。你可以在外边加判断。判断提交之后再显示。本回答被提问者采纳
第3个回答  2009-05-14
<?php
$name = $_POST['name'];
echo $name;
?>

注意,里面使用单引号和双引号都可以,不用引号是错误的。
第4个回答  2009-05-14
$_REQUEST['函数名']
这个是最好的,包含post和get两种方式的接受

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网