admin管理员组文章数量:1305106
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 = '';
//$tabUrl will open fer.php
$linkUrl = $tabUrl . '&app_data=' . $encodedParams;
?>
...
function loopPage()
{
top.location = "<?= $linkUrl ?>";
//reloads fer.php
}
Reference: /
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!
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./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./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!
- So, basically, you're trying to send data from a page with HTML/JavaScript to a PHP script? – Ja͢ck Commented Jun 14, 2012 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 Commented Jun 14, 2012 at 10:10
3 Answers
Reset to default 4It's still not clear whether you want to:
pass data that you receive in PHP via
app_data
to JavaScript, orpass 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() {
// ...
}
});
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>
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.
本文标签: Passing a JavaScript Value to a PHP Variable (With Limitation)Stack Overflow
版权声明:本文标题:Passing a JavaScript Value to a PHP Variable (With Limitation) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741797685a2398033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论