登录  /  注册

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

php中文网
发布: 2016-06-13 12:11:57
原创
710人浏览过

关于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 />
登录后复制


handle.php
<br /><?php<br />$name = isset($_POST['name'])? $_POST['name'] : '';<br />file_put_contents('tt.txt', $name, true);<br />echo 'success';<br />?><br />
登录后复制


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 />
登录后复制

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号