简体   繁体   English

PHP中的Window.open,而循环无法正常运行

[英]Window.open within php while loop not functioning correctly

I have a while loop that creates multiple tables with information inside. 我有一个while循环,该循环创建带有内部信息的多个表。 Within each table is a facebook and twitter icon. 每个表中都有一个Facebook和Twitter图标。 A reader can share the info or tweet the info that's within the table via an onclick event. 读者可以通过onclick事件共享信息或在表中发布信息。

Here's each event. 这是每个事件。

<?php
echo'<script type="text/javascript">
function tweet(){

var sharer = "https://twitter.com/intent/tweet?source=webclient&text=',$title2,' - http://www.globatum.com/lite/post.php?id=',$row['id'],'";
window.open(sharer,\'sharer\' , \'width=600,height=500\');}
</script>';
?>
<?php
echo'<script type="text/javascript">
function fbshare(){

var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.globatum.com/lite/post.php?id=',$row['id'],'";
window.open(share,\'share\' , \'width=600,height=500\');}
</script>';
?>

The odd thing is that it works. 奇怪的是它有效。 somewhat. 有些。 When I click the fb icon a small window with the dialogue will pop up. 当我单击fb图标时,将弹出一个带有对话框的小窗口。 Same for twitter. 推特也一样。 The problem is that when I look at the header of the popup... it's using the same info from all of the tables. 问题是当我查看弹出窗口的标题时...它正在使用所有表中的相同信息。

What I want... 我想要的是...

Table 1 fb icon clicks should open window https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1 表1 FB图标点击应打开窗口https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1

table 2 will be https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=2 表2将为https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=2

table 3 .. https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=3 etc etc 表3 .. https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=3

however when I click on the icons... 但是,当我单击图标时...

table 1: opens window https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1 (GOOD) 表1:打开窗口https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1(GOOD

table 2: opens window... https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1 (BAD) 表2:打开窗口... https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1(BAD

table 3: opens window... https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1 (BAD) 表3:打开窗口... https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=1(BAD

It sounds like a simple sql issue but when I pull up the page source I get 听起来像是一个简单的sql问题,但是当我拉出页面源代码时,我得到了

Table 1 表格1

<script type="text/javascript">
function tweet(){

var sharer = "https://twitter.com/intent/tweet?source=webclient&text=random title - http://www.makeupsite.com/post.php?id=39";
window.open(sharer,'sharer' , 'width=600,height=500');}
</script><script type="text/javascript">
function fbshare(){

var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=39";
window.open(share,'share' , 'width=600,height=500');}
</script>

Table 2 表2

<script type="text/javascript">
function tweet(){

var sharer = "https://twitter.com/intent/tweet?source=webclient&text=random title - http://www.makeupsite.com/post.php?id=43";
window.open(sharer,'sharer' , 'width=600,height=500');}
</script><script type="text/javascript">
function fbshare(){

var share = "https://www.facebook.com/sharer/sharer.php?u=http://www.makeupsite.com/post.php?id=43";
window.open(share,'share' , 'width=600,height=500');}
</script>

Basically the page source is saying one thing...but the window.open is doing something completely different. 基本上页面源在说一件事...但是window.open在做完全不同的事情。

You're redefining the same function over and over and over again. 您一次又一次地重新定义相同的功能。 Depending on the browser, you'll always call either the first or last instance of tweet() or fbshare() you define. 根据浏览器的不同,您将始终调用您定义的tweet()或fbshare()的第一个或最后一个实例。

In the footer of your doc, set up a single function: 在文档的页脚中,设置一个功能:

function shareLink(targetURL){
    window.open(targetURL, 'share', 'width=600,height=500');
}

Then, tie that to your social buttons: 然后,将其绑定到您的社交按钮:

onclick=shareLink("[twitter URL here]");

or 要么

onclick=shareLink("[facebook URL here]");

See what I'm getting at? 明白我在说什么吗?

EDIT whoops, helps if I get the names right 编辑哎呀,如果我的名字正确的话会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM