[英]how to pass a php variable in a href link with javascript window open
The code below works to call a JS window.open 下面的代码可以调用JS窗口。
<?
$facebookID = "ID";
$parameters = array(
'app_id' => 'app_ID',
'to' => $facebookID,
'link' => 'link',
'redirect_uri' => 'my_url'
);
$url = 'http://www.facebook.com/dialog/send?'.http_build_query($parameters);
echo '<script type="text/javascript">window.open('.json_encode($url).');</script>';
?>
CODE ABOVE WORKS... it opens a window with the facebook send dailog 上面的代码...它会打开一个窗口,其中包含facebook send dailog
But how can I implement this in a link to call this window on click only with the specefic facebook ID... 但是我该如何在链接中实现此功能,以仅在使用特定的Facebook ID单击时调用此窗口...
I build my page with multiple faceboook ID like this: 我使用多个faceboook ID构建页面,如下所示:
<?
// CODE ABOVE.....
while($row = $stmt->fetch())
{
$facebookID = $row['facebookID'];
$parameters = array(
'app_id' => 'app_ID',
'to' => $facebookID,
'link' => 'link',
'redirect_uri' => 'my_url'
);
$url = 'http://www.facebook.com/dialog/send?'.http_build_query($parameters);
?>
<a href="#">ON CLICK CALL the JavaScript for this specefic Facebook User</a>
<?
}
?>
What should be my href="....." ? 我的href =“ .....”应该是什么? I tried the following, but it didn't work... I guess it's because of the php variable inside the window.open...
我尝试了以下操作,但是没有用...我想这是由于window.open中的php变量导致的...
<a href="javascript: void(0)" onclick="window.open('.json_encode($url).');" </a>
thanks 谢谢
尝试这个:
echo '<a href="#" onclick="window.open(' . json_encode($url) . '); return false;">ON CLICK CALL the JavaScript for this specefic Facebook User</a>';
You cannot use json encode inside of window.open with double quotes in your attributes because the json_encode will add double quotes to your code. 您不能在window.open内部使用属性中带有双引号的json编码,因为json_encode会在代码中添加双引号。
(see that the attribute onclick is "fighting" with your double quotes from window.open) (请注意,属性onclick与window.open中的双引号“发生冲突”)
If you invert the single quotes to double quotes, it will work properly using json_encode. 如果将单引号转换为双引号,则可以使用json_encode正常工作。
$url = 'http://test.com';
echo "<a href='javascript: void(0)' onclick='window.open(" . json_encode($url) . ");'>Test</a>";
(because the json encode will add double quotes and your attributes have single quotes.) (因为json编码将添加双引号,并且您的属性具有单引号。)
Another way to do it (that i recommend) is remove the json_encode and put single quotes inside your php. 另一种方法 (我建议)是删除json_encode并将单引号放在您的php中。
$url = 'http://test.com';
echo '<a href="javascript: void(0)" onclick="window.open(' . '\'' . $url . '\'' . ');">Test</a>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.