简体   繁体   English

如何在打开JavaScript窗口的href链接中传递php变量

[英]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.

相关问题 如何从javaScript中的url(href链接)获取php变量,并在鼠标悬停时使用传递的php数据生成一个小的弹出窗口 - How to get php variable from url (href link) in javaScript and generate a small popup window on mouseover with passed php data 如何在javascript中将变量传递给href? - How to pass variable to href in javascript? PHP传递变量到新的Window.Open Javascript - PHP Pass Variable to new Window.Open Javascript 我正在尝试将一些 PHP 变量和 Javascript 变量传递到 Javascript window.location.href 中的 url - I am trying to pass some PHP variables and Javascript variable into url in Javascript window.location.href 如何在PHP和JavaScript中重定向PHP Window.location.href或Window.open()方法 - How redirect php Window.location.href or Window.open () methods in php and JavaScript 如何使用href链接将javascript变量作为参数传递? - How to pass javascript variables as parameters with a href link? Javascript 如何使用 window.open 将变量传递给 URL 查询 - Javascript how to pass variable to URL query with window.open 如何在window.open方法中将动态PHP变量作为参数传递 - How to pass dynamic PHP variable as parameter in window.open method 如何在window.open函数中传递php变量 - How to pass a php variable in window.open function 通过open.window传递php变量 - Pass php variable though open.window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM