将JavaScript值传递给PHP变量(限制) [英] Passing a JavaScript Value to a PHP Variable (With Limitation)

查看:142
本文介绍了将JavaScript值传递给PHP变量(限制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道现有的问题和答案已经解决了这个问题。我已经看过他们,但我的情况并不允许我应用这些线程中提供的解决方案。



鉴于JavaScript是客户端,PHP是server-这些是提供的两种解决方案:



GET / POST方法(Post#3)



AJAX方法



限制: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)



将JSON数据从Javascript发送到PHP?



如何将数据从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:

GET/POST Method (Post #3)

AJAX Method


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:

  1. pass data that you receive in PHP via app_data to JavaScript, or

  2. pass 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆