Home > Backend Development > PHP Tutorial > 关于POST方式传递数据的有关问题

关于POST方式传递数据的有关问题

WBOY
Release: 2016-06-13 12:11:57
Original
850 people have browsed it

关于POST方式传递数据的问题
最近在研究怎么用POST方式传递数据
我想得到的效果是,打开网页a,向一个网页b传递一个字符串
然后在网页b上显示这个字符串。
在网上找到一份代码,是这样的
网页a的代码:
$uri = "http://localhost/handle.php";
// 参数数组
$data = array (
        'name' => 'tanteng' 
// 'password' => 'password'
);
 
$ch = curl_init ();
// print_r($ch);
curl_setopt ( $ch, CURLOPT_URL, $uri );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$return = curl_exec ( $ch );
curl_close ( $ch );
 
print_r($return);
?>

网页b的代码
echo 'this is the posted data';
if(isset($_POST['name'])){
    if(!empty($_POST['name'])){
        echo '您好,',$_POST['name'].'!';
    }
}
?>

在我电脑上实验的结果是打开网页b只显示  this is the posted data
而网页a上显示    您好,tanteng!
有谁能给我解释下为什么吗?如果我想让这句话在网页b上显示该怎么做?
------解决思路----------------------
可以用數據庫或文件保存。

a.php

<br /><?php<br />$uri = "http://localhost/handle.php";<br />// 参数数组<br />$data = array (<br />        'name' => 'tanteng' <br />// 'password' => 'password'<br />);<br /> <br />$ch = curl_init ();<br />// print_r($ch);<br />curl_setopt ( $ch, CURLOPT_URL, $uri );<br />curl_setopt ( $ch, CURLOPT_POST, 1 );<br />curl_setopt ( $ch, CURLOPT_HEADER, 0 );<br />curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );<br />curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );<br />$return = curl_exec ( $ch );<br />curl_close ( $ch );<br /> <br />print_r($return);<br />?><br />
Copy after login


handle.php
<br /><?php<br />$name = isset($_POST['name'])? $_POST['name'] : '';<br />file_put_contents('tt.txt', $name, true);<br />echo 'success';<br />?><br />
Copy after login


b.php
<br /><?php<br />echo 'this is the posted data';<br />if(file_exists('tt.txt')){<br />	$name = file_get_contents('tt.txt');<br />    if(!empty($name)){<br />        echo '您好,',$name.'!';<br />    }<br />}<br />?><br />
Copy after login

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template