0

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!

Community
  • 1
  • 1
Mysophobe
  • 622
  • 2
  • 10
  • 33
  • So, basically, you're trying to send data from a page with HTML/JavaScript to a PHP script? – Ja͢ck Jun 14 '12 at 10:04
  • Yes, I'm trying to send data back to the same page so as to pass the data from client-side to server-side. To make sure I didn't get your question wrongly, the HTML/JavaScript and PHP are in the same file(fer.php). – Mysophobe Jun 14 '12 at 10:10

3 Answers3

4

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() {
        // ...
    }
});
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • I'm trying to pass data from JavaScript to PHP. The Ajax method would not work for me as the Facebook Page Tab application that I'm developing doesn't allow me to access the query string except for the one and only app_data parameter. This is where app_data comes in and this app_data have to be used together with Facebook signed_request which is encoded by JSON. – Mysophobe Jun 14 '12 at 10:33
  • 1
    @Zany your php script can be reached directly using Ajax, it doesn't have to go via Facebook first; in fact, that's impossible due to cross-domain – Ja͢ck Jun 14 '12 at 10:50
  • I tried passing from fer.php to form.php but the parameter varA could not be retrieved in form.php. (fer.php) http://jsfiddle.net/ADdrR/ (form.php) http://jsfiddle.net/cwxXY/ – Mysophobe Jun 14 '12 at 14:47
  • @Zany sorry, but I have no clue what you're doing anymore =/ – Ja͢ck Jun 14 '12 at 14:58
  • @Zany no idea, from here onwards you have to check the network activity using your browser's developer tools, such as Chrome/Safari/Firebug. – Ja͢ck Jun 14 '12 at 15:25
  • I made a mistake, you were right about "it doesn't have to go via Facebook first". Using Ajax solved the issue of passing of values from JavaScript to PHP without Facebook's intervention. Thank you for your help! :) – Mysophobe Jun 15 '12 at 08:08
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>   
patricksweeney
  • 3,939
  • 7
  • 41
  • 54
RKINFOPHP
  • 21
  • 2
-1

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.

Community
  • 1
  • 1
CBroe
  • 91,630
  • 14
  • 92
  • 150
  • I've looked through GET/POST and Ajax methods. However, these methods cannot be used as "Facebook Page Tabs are loaded in an iFrame, they don't have access to query string". The only workaround provided by Facebook is through Facebook signed_request. – Mysophobe Jun 14 '12 at 10:11
  • I’m still not clear on what data you’re actually trying to access. If it’s just query string parameters Facebook appends to the address while initially loading your app in the iframe – well of course JS can access them; but there would be no need to, because they happen to be transferred to the server side first anyway. – CBroe Jun 14 '12 at 10:15
  • With reference to the last section of my question, I'm trying to access the variables in my someValues() JavaScript function and assign them to my PHP variables. However, this is not possible since the data has to be sent to the server first, thus the whole thingy of passing parameters to another/same page. – Mysophobe Jun 14 '12 at 10:26
  • But _why_? Where do these values come from, what’s their connection to the query string …? – CBroe Jun 14 '12 at 11:40
  • The flow goes like this, when the page loads, my JavaScript function will compute 2 values based on the user's location. In the same page, my application will then post these 2 values to the user's Facebook wall using PHP SDK. The problem I'm facing is passing the 2 JavaScript values over to PHP side. And one of the way to achieve this is to use query string – Mysophobe Jun 14 '12 at 15:22