title: Passing a JavaScript Value to a PHP Variable (With Limitation)
body: <p>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.</p>
  
  <p>Given that JavaScript is client-side and PHP is server-side, these are the 2 solutions offered:</p>
  
  <p><a href="http://tycoontalk.freelancer.com/php-forum/20054-how-do-i-assign-javascript-variable.html" rel="nofollow noreferrer">GET/POST Method (Post #3)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/6139663/how-to-pass-a-value-to-php-variable-by-ajax">AJAX Method</a>
  <hr />
  <strong>Limitation: Facebook Page Tab Application</strong></p>
  
  <p>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 <code>app_data</code> GET parameter in Facebook <code>signed_request</code> object together with JSON encoding. 
  <hr />
  <strong>Solving the Limitation: Using app_data GET Parameter</strong></p>
  
  <p>I'm able to pass parameters to the same page by loading the same page again but <strong>I'm still only dealing with PHP variables and values</strong>.</p>
  
  <p><strong>fer.php</strong></p>
  
  <pre><code>&lt;?php
  $appData = array();
  if (!empty($signedRequest) &amp;&amp; !empty($signedRequest['app_data'])) {
      $appData = json_decode($signedRequest['app_data'], true);
  }
  
  echo '&lt;pre&gt;' . print_r($appData) .'&lt;/pre&gt;';
  //prints Array ( [lat] =&gt; 123 [lon] =&gt; 456 ) when fer.php reloads
  
  $params = array(
  'lat' =&gt; '123',
  'lon' =&gt; '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 . '&amp;app_data=' . $encodedParams;
  ?&gt;
  ...
  function loopPage() 
  {
          top.location = "&lt;?= $linkUrl ?&gt;";
          //reloads fer.php
  } 
  </code></pre>
  
  <p>Reference: <a href="http://labs.thesedays.com/blog/2011/06/23/query-strings-for-facebook-page-tabs/" rel="nofollow noreferrer">http://labs.thesedays.com/blog/2011/06/23/query-strings-for-facebook-page-tabs/</a></p>
  
  <hr />
  
  <p><strong>Also Tried:</strong> </p>
  
  <p><a href="http://iamskwerl.com/tech/2011/11/passing-query-variables-to-facebook-fan-page-tabs/" rel="nofollow noreferrer">Cookie Method</a> (Dropped as I'm still only dealing with PHP variables and values)</p>
  
  <p>Using JavaScript <code>encodeURIComponent()</code> and <code>encodeURI()</code> (Wrong approach as these methods encode the whole URL and the parameters passed over are not recognized by <code>json_decode</code>)</p>
  
  <p><strong>Currently Trying:</strong> JSON with Ajax (XMLHttpRequest)</p>
  
  <p><a href="https://stackoverflow.com/questions/8599595/send-json-data-from-javascript-to-php">Send JSON data from Javascript to PHP?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/406316/how-to-pass-a-variable-data-from-javascript-to-php-and-vice-versa">How to pass data from Javascript to PHP and vice versa?</a>
  <hr />
  <strong>What I'm really trying to achieve:</strong></p>
  
  <p>PHP variables getting JavaScript values in the <strong>same page</strong> in a <strong>Facebook iFrame</strong>. It is all right for the page to reload to pass parameter to itself.</p>
  
  <p><strong>fer.php</strong> (Wrong Example)</p>
  
  <pre><code>function someValues(){
      varA = 123;
      varB = 456;
  }
  
  &lt;?php
      $cupA = varA;
      $cupB = varB;
  ?&gt;
  </code></pre>
  
  <p>I've been trying to solve this problem for days and I'm going nuts to the extend that I even try to trick <code>json_decode</code> by adding <code>%</code> to my data before appending it to a redirect URL =x I really appreciate any help or direction given. Thanks!</p>