Javascript 和 PHP (window.open)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10249560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript and PHP (window.open)
提问by Ricardo Jerónimo
So, in my website, I have a news system which has an option to edit and delete the news the administrator desires.
因此,在我的网站中,我有一个新闻系统,可以选择编辑和删除管理员想要的新闻。
Well, I got the edit part right by using:
好吧,我通过使用以下方法获得了正确的编辑部分:
href="noticiaEditarForm.php?id_noticia=<?php echo $id ?>">Editar</a>
href="noticiaEditarForm.php?id_noticia=<?php echo $id ?>">Editar</a>
And then a $_GET
on the other page.
然后$_GET
在另一页上。
However, this is not how I desire my editing window. Therefore, I have been exploring a way to send the PHP variable that contains the primary key for the news table (MySQL) to a popup window, using JavaScript. But that's just the thing, it will only return the 1st value it gets from the query... (i.e If I click to edit the 3rd article, it edits my 1st one. Always.)
然而,这不是我想要的编辑窗口。因此,我一直在探索一种使用 JavaScript 将包含新闻表 (MySQL) 主键的 PHP 变量发送到弹出窗口的方法。但这就是问题,它只会返回它从查询中获得的第一个值......(即如果我点击编辑第三篇文章,它会编辑我的第一篇文章。总是。)
Here is my current code:
这是我当前的代码:
<div class="noticias">
<?php
include('conn/conn.php');
mysql_select_db($bd, $conn);
$resultado = mysql_query("SELECT * FROM noticia INNER JOIN user ON noticia.id_user=user.id_user ORDER BY id_noticia DESC");
while ($linha = mysql_fetch_array($resultado)) {
echo "<h1>" . $linha['titulo'] . "</h1>";
echo "<i>Posted by " .$linha['username']. " on " . "<y>" . $linha['data'] . "</y>" . "</i>";
echo "<p>";
echo $linha['texto'];
$id = $linha['id_noticia'];
if (isset($_SESSION['admin'])) {
?>
<div class="noticiasOpcao">
<a href="" onClick="open_win_editar()">Editar</a>
<a onclick="return confirm('Are you sure?')" href="noticiaApagar.php?id_noticia=<?php echo $id ?>">Apagar</a>
</div>
<?php
}
}
?>
<script language="javascript">
function open_win_editar() {
window.open (
"noticiaEditarForm.php?id_noticia=<?php echo $id; ?>",
"Editar notícia",
"location=1, status=1, scrollbars=1, width=800, height=455"
);
}
</script>
<?php mysql_close($conn); ?>
</div>
My point is to then use another query to get the title and text of the article to display on an WYSIWYG editor.
我的观点是然后使用另一个查询来获取文章的标题和文本以显示在 WYSIWYG 编辑器上。
Can anyone point out my flaw?
谁能指出我的缺点?
回答by David
This code:
这段代码:
<script language="javascript">
function open_win_editar() {
window.open ("noticiaEditarForm.php?id_noticia=<?php echo $id; ?>", "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
}
</script>
Is happening outside of the PHP while
loop, so the value of $id
will be the last value that was set to $id
in the loop. So the JavaScript code will always open the same link.
发生在 PHPwhile
循环之外,因此 的值$id
将是$id
在循环中设置的最后一个值。因此 JavaScript 代码将始终打开相同的链接。
If you need the code within the PHP loop to specify the $id
value for the JavaScript, then you can pass it as an argument to the JavaScript function. Something like this:
如果您需要 PHP 循环中的代码来指定$id
JavaScript的值,那么您可以将其作为参数传递给 JavaScript 函数。像这样的东西:
<script language="javascript">
function open_win_editar(targetID) {
window.open ("noticiaEditarForm.php?id_noticia=" + targetID, "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
}
</script>
So the code rendering the anchor tags in the loop would pass the argument like this:
所以在循环中渲染锚标记的代码将传递这样的参数:
<a href="" onClick="open_win_editar(<?php echo $id; ?>)">Editar</a>
The rendered output would then contain the record-specific $id
value on each a
tag to be used by the JavaScript code on the client.
然后,呈现的输出将包含$id
每个a
标记上特定于记录的值,以供客户端上的 JavaScript 代码使用。
回答by Stefan Dochow
The id of the piece to edit is only updated within the while loop and never outside of it.
要编辑的片段的 id 仅在 while 循环内更新,而不会在循环之外更新。
To use it as you wish you should use a parameter for the open_with_editar function:
要按照您的意愿使用它,您应该为 open_with_editar 函数使用一个参数:
<script language="javascript">
function open_win_editar(id) {
window.open ("noticiaEditarForm.php?id_noticia="+id, "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
}
</script>
Now you only have to update the onclick event and hand over teh respective id:
现在你只需要更新 onclick 事件并交出各自的 id:
<div class="noticiasOpcao">
<a href="" onClick="open_win_editar(<?php echo $id; ?>)">Editar</a>
That should work.
那应该工作。
Regards STEFAN
问候斯蒂芬
回答by Ricardo Jerónimo
<script language="javascript">
function open_win_editar() {
window.open ("noticiaEditarForm.php?id_noticia=<?php echo $id; ?>", "Editar notícia", "location=1, status=1, scrollbars=1, width=800, height=455");
}
</script>
This is only rendered once, probably with the last ID. You need to figure out a structure so you can pass a different ID to it based on what article you clicked to edit.
这仅呈现一次,可能使用最后一个 ID。您需要找出一个结构,以便根据您单击编辑的文章将不同的 ID 传递给它。