将 JavaScript 值传递给 PHP 变量(有限制)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11029298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:52:00  来源:igfitidea点击:

Passing a JavaScript Value to a PHP Variable (With Limitation)

phpjavascriptfacebookjsonfacebook-iframe

提问by Zany

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:

鉴于 JavaScript 是客户端而 PHP 是服务器端,以下是提供的 2 个解决方案:

GET/POST Method (Post #3)

GET/POST 方法(帖子 #3)

AJAX Method

AJAX 方法



Limitation: Facebook Page Tab Application限制:Facebook 页面标签应用程序

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_dataGET parameter in Facebook signed_requestobject together with JSON encoding.

我无法使用上述通过 URL 传递参数的方法,因为 Facebook 页面选项卡已加载到 iFrame 中,它们无权访问查询字符串。Facebook 通过使用app_dataFacebooksigned_request对象中的GET 参数和 JSON 编码提供了一种解决方法。



Solving the Limitation: Using app_data GET Parameter解决限制:使用 app_data GET 参数

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.

我可以通过再次加载同一页面将参数传递到同一页面,但我仍然只处理 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>';
//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/

参考: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)

Cookie 方法(已删除,因为我仍然只处理 PHP 变量和值)

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)

使用 JavaScriptencodeURIComponent()encodeURI()(错误的方法,因为这些方法对整个 URL 进行编码,并且传递的参数不被 识别json_decode

Currently Trying:JSON with Ajax (XMLHttpRequest)

目前正在尝试:JSON with Ajax (XMLHttpRequest)

Send JSON data from Javascript to PHP?

将 JSON 数据从 Javascript 发送到 PHP?

How to pass data from Javascript to PHP and vice versa?

如何将数据从 Javascript 传递到 PHP,反之亦然?



What I'm really trying to achieve:我真正想要达到的目标:

PHP variables getting JavaScript values in the same pagein a Facebook iFrame. It is all right for the page to reload to pass parameter to itself.

PHP 变量在Facebook iFrame同一页面中获取 JavaScript 值。页面重新加载以将参数传递给自身是可以的。

fer.php(Wrong Example)

fer.php(错误示例)

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_decodeby adding %to my data before appending it to a redirect URL =x I really appreciate any help or direction given. Thanks!

几天来我一直在尝试解决这个问题,我什至试图json_decode通过%在将数据附加到重定向 URL =x 之前添加到我的数据来欺骗我,我真的很感激给出的任何帮助或指示。谢谢!

回答by Ja?ck

It's still not clear whether you want to:

目前还不清楚您是否想要:

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

  2. pass data from JavaScript to PHP.

  1. 将您在 PHP 中收到的数据通过app_dataJavaScript传递,或

  2. 将数据从 JavaScript 传递到 PHP。

Pass data from PHP to JavaScript

将数据从 PHP 传递到 JavaScript

<head>
<script type="text/javascript">
var app_data = <?php echo json_encode($appData); ?>;
</script>
</head>
<body>
...

Pass data from JavaScript to PHP

将数据从 JavaScript 传递到 PHP

$.ajax({
    url: location.pathname, // current page
    type: 'POST',
    data: {
        vara: 'hello',
        varb: 'world'
    },
    success: function() {
        // ...
    }
});

回答by RKINFOPHP

You can do that easily with javascript for example HTML code:

您可以使用 javascript 轻松做到这一点,例如 HTML 代码:

<select id = "Select" onchange = "FijarPrecio (this.id);" >
    <option value = "10"> product 1 </ option>
    <option value = "20"> product 2 </ option>
    <option value = "30"> product 3 </ option>
</select>
<input type = "text"  name = "Price" id = "Price"/>
<script type = "text/javascript" >
    FijarPrecio function (id) {
       PrecioSel var = document.getElementById (id);
       PrecioActual var = document.getElementById ('Price');
       PrecioActual.value = PrecioSel.value;
    }
</script>   

回答by CBroe

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.

我真正想要达到的目标:

PHP 变量在 Facebook iFrame 的同一页面中获取 JavaScript 值。页面重新加载以将参数传递给自身是可以的。

Easiest way: Generate a form element, populate it with some (hidden) input fields, and POST it to your server.

最简单的方法:生成一个表单元素,用一些(隐藏的)输入字段填充它,然后将它发布到您的服务器。

AJAX is also possible, and maybe nicer for the user as it doesn't require a reload.

AJAX 也是可能的,并且可能对用户更好,因为它不需要重新加载。

Since your're not describing any problem in detail, I can only assume it's mostly due to a general lack of knowledge/experience in these matters on your part. So maybe look for some tutorials first, to get a general understanding of the techniques involved.

由于您没有详细描述任何问题,我只能假设这主要是由于您在这些问题上普遍缺乏知识/经验。因此,也许可以先查找一些教程,以大致了解所涉及的技术。