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

做~自己de王妃 提交于 2019-11-27 22:52:59

问题


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!


回答1:


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() {
        // ...
    }
});



回答2:


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

<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>   



回答3:


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.

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.

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.



来源:https://stackoverflow.com/questions/11029298/passing-a-javascript-value-to-a-php-variable-with-limitation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!