将JavaScript值传递给PHP变量(限制) [英] Passing a JavaScript Value to a PHP Variable (With Limitation)
问题描述
鉴于JavaScript是客户端,PHP是server-这些是提供的两种解决方案:
限制:Facebook页面选项卡应用程序
我不能使用上述通过URL传递参数的方法,因为Facebook页面标签加载在iFrame中,他们无法访问查询字符串。 Facebook通过使用Facebook signed_request
对象中的 app_data
GET参数与JSON编码一起提供了解决方法。
解决限制:使用app_data GET参数
我可以传递参数通过加载相同的页面,同一页面,但我仍然只处理PHP变量和值。
fer.php
<?php
$ appData = array();
if(!empty($ signedRequest)&&!empty($ signedRequest ['app_data'])){
$ appData = json_decode($ signedRequest ['app_data'],true);
}
echo'< pre>'。 print_r($ appData)。'< / pre>';
//打印数组([lat] => 123 [lon] => 456)当fer.php重新加载$ b
$ b $ params = array(
'lat'= >'123',
'lon'=>'456'
);
$ encodedParams = urlencode(json_encode($ params));
$ tabUrl ='http://www.facebook.com/pages/FACEBOOK_PAGE/367117263337064?sk=app_433576149993619';
// $ tabUrl将打开fer.php
$ linkUrl = $ tabUrl。 '& app_data ='。 $ encodedParams;
?>
...
function loopPage()
{
top.location =<?= $ linkUrl?>;
// reloads fer.php
}
参考: http://labs.thesedays.com/blog / 2011/06/23 / query-strings-for-facebook-page-tabs /
还尝试了
Cookie方法(丢弃,因为我仍然只处理PHP变量和值)
使用JavaScript encodeURIComponent()
和 encodeURI()
(错误的方法,因为这些方法编码整个URL和
目前正在尝试: JSON与Ajax(XMLHttpRequest)
如何将数据从Javascript传递给PHP,反之亦然?
我真正想要实现的目标:
PHP变量在相同页面中获取JavaScript值, strong> Facebook iFrame 。
fer.php (错误的示例)
function someValues(){
varA = 123;
varB = 456;
}
<?php
$ cupA = varA;
$ cupB = varB;
?>
我一直在努力解决这个问题,而且我会继续坚持我甚至尝试通过在我的数据中添加%
来欺骗 json_decode
,然后再将其附加到重定向网址= x我非常感谢任何帮助或指示。谢谢!
目前还不清楚是否要:
- app_data 将您在PHP中收到的数据传递给JavaScript或
<将数据从JavaScript传递给PHP
将数据从PHP传递给JavaScript
< head>
< script type =text / javascript>
var app_data =<?php echo json_encode($ appData); ?取代;
< / script>
< / head>
< body>
...
将数据从JavaScript传递给PHP
$。ajax({
url:location.pathname,//当前页
类型:'POST',
数据:{
vara:'hello',
varb:'world'
},
success:function(){
// ...
}
});
I understand that there are existing questions and answers that have already addressed this matter. I've looked through them but my situation doesn't allow me to apply the solutions offered in these threads.
Given that JavaScript is client-side and PHP is server-side, these are the 2 solutions offered:
Limitation: Facebook Page Tab Application
I cannot use the above methods of passing parameters through URL as Facebook Page Tabs are loaded in an iFrame, they don't have access to query string. Facebook provides a workaround by using the app_data
GET parameter in Facebook signed_request
object together with JSON encoding.
Solving the Limitation: Using app_data GET Parameter
I'm able to pass parameters to the same page by loading the same page again but I'm still only dealing with PHP variables and values.
fer.php
<?php
$appData = array();
if (!empty($signedRequest) && !empty($signedRequest['app_data'])) {
$appData = json_decode($signedRequest['app_data'], true);
}
echo '<pre>' . print_r($appData) .'</pre>';
//prints Array ( [lat] => 123 [lon] => 456 ) when fer.php reloads
$params = array(
'lat' => '123',
'lon' => '456'
);
$encodedParams = urlencode(json_encode($params));
$tabUrl = 'http://www.facebook.com/pages/FACEBOOK_PAGE/367117263337064?sk=app_433576149993619';
//$tabUrl will open fer.php
$linkUrl = $tabUrl . '&app_data=' . $encodedParams;
?>
...
function loopPage()
{
top.location = "<?= $linkUrl ?>";
//reloads fer.php
}
Reference: http://labs.thesedays.com/blog/2011/06/23/query-strings-for-facebook-page-tabs/
Also Tried:
Cookie Method (Dropped as I'm still only dealing with PHP variables and values)
Using JavaScript encodeURIComponent()
and encodeURI()
(Wrong approach as these methods encode the whole URL and the parameters passed over are not recognized by json_decode
)
Currently Trying: JSON with Ajax (XMLHttpRequest)
Send JSON data from Javascript to PHP?
How to pass data from Javascript to PHP and vice versa?
What I'm really trying to achieve:
PHP variables getting JavaScript values in the same page in a Facebook iFrame. It is all right for the page to reload to pass parameter to itself.
fer.php (Wrong Example)
function someValues(){
varA = 123;
varB = 456;
}
<?php
$cupA = varA;
$cupB = varB;
?>
I've been trying to solve this problem for days and I'm going nuts to the extend that I even try to trick json_decode
by adding %
to my data before appending it to a redirect URL =x I really appreciate any help or direction given. Thanks!
It's still not clear whether you want to:
pass data that you receive in PHP via
app_data
to JavaScript, orpass data from JavaScript to PHP.
Pass data from PHP to JavaScript
<head>
<script type="text/javascript">
var app_data = <?php echo json_encode($appData); ?>;
</script>
</head>
<body>
...
Pass data from JavaScript to PHP
$.ajax({
url: location.pathname, // current page
type: 'POST',
data: {
vara: 'hello',
varb: 'world'
},
success: function() {
// ...
}
});
这篇关于将JavaScript值传递给PHP变量(限制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!