我将myid传递到url.php页面,并通过ajax请求加载该页面。同时,我想将myid发布到除url.php之外的其他文件。比如x.php,y.php。
FB.api('/me?fields=movies,email,name', function(mydata) {
var email=mydata.email;
var json = JSON.stringify(mydata.movies.data);
var a = JSON.parse(json);
$.post('movies_db.php',{'myd':a, name: name, email: email, myid:myid}, function(data)
{
$.ajax({
url:'url.php'
,async: true
,type : 'POST'
,cache: false
,data : 'myid=' + myid //Here I post myid to url.php. How can I post it to other files like x.php , y.php on same moment?
,dataType: 'html'
,success: function(data){
$('body').html(data);
FB.XFBML.parse();
}
}
);
}
);底线是我想使用当前的user_id e.i。在用户执行登录操作后,在各个页面上进行myid。
更新
我将变量从index.php发布到url.php。inside index.php:$.ajax({ url:'url.php‘),异步: true,键入:'POST’,缓存: false,数据:'myid=‘+ myid //这里我将myid发布到url.php。如何在同一时间将其发布到其他文件,如x.php、y.php?、dataType:'html‘、成功:函数(数据){}
在url.php中,我将提到:
<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['myid'];
?>现在在url.php中,假设我有:
<script>
function funInclude()
{
include "data1.php";
include "data2.php";
}
</srcipt>所以data1.php和daa2.php都会重新启动myid,对吗?
发布于 2013-10-21 05:11:24
你有两个选择。
url.php页面中,只需包含另外两个php页面,它们也将获得变量数据如果您选择了选项2,我建议您在函数调用中进行包含,以便将这些页面的变量与调用页部分分离开来。
编辑:解释选项2并将其放入函数调用:
在PHP中,当您包含一个文件时,您不仅仅是在“调用”另一个页面,还包括代码,就像它是当前正在运行的页面的一部分一样。它们将作为一页汇编在一起。这可能会导致不可预见的问题,因为page 1 (包括page 2 )可能有一个与page 2和page 2可能更改该变量同名的变量。这将导致在page 1上出现一个意外的变量值。
为了说明这一点,PHP允许您(部分)通过调用函数中的include来限制include页面及其变量的范围。
"calling.php"示例
<?php
//This included page's variables are accessible as though they were in this "calling.php" page
include "included.php";
function restrictScope()
{
//This page's variables/changes are only accessible inside this function
include "included2.php";
}http://php.net/manual/en/function.include.php
编辑:完整的工作示例
这是基本页面: include.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="include.php" method="POST">
<input type="hidden" name="myid" value="3" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
<?php
if ( $_SERVER[ "REQUEST_METHOD" ] == "POST" )
{
//Let's call included.php
passOff();
//Now let's check it actually did it
$savedId = ( file_exists( "myid.txt" ) ) ? file_get_contents( "myid.txt" ) : "null";
echo "MyId: " . $savedId;
}
function passOff()
{
include "included.php";
}
?>这是包含的页面: included.php
<?php
if ( $_SERVER[ "REQUEST_METHOD" ] == "POST" )
{
//Create and write the "myid"
file_put_contents( "myid.txt", $_POST[ "myid" ] );
}
?>的重要性:要使我的示例工作,您必须在您的web文件夹中具有写权限。但是,即使文件写入失败,包含的页面仍然会获得已发布的变量。
https://stackoverflow.com/questions/19486505
复制相似问题