Javascript 和 PHP (window.open) [英] Javascript and PHP (window.open)
问题描述
因此,在我的网站中,我有一个新闻系统,可以选择编辑和删除管理员想要的新闻.
好吧,我通过使用:
href="noticiaEditarForm.php?id_noticia=<?php echo $id ?>">Editar</a>
然后是另一个页面上的 $_GET
.
然而,这不是我想要的编辑窗口.因此,我一直在探索一种使用 JavaScript 将包含新闻表 (MySQL) 主键的 PHP 变量发送到弹出窗口的方法.但这就是事情,它只会返回它从查询中获得的第一个值......(即如果我点击编辑第 3 篇文章,它会编辑我的第 1 篇文章.总是.)
这是我当前的代码:
<?php包括('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");而 ($linha = mysql_fetch_array($resultado)) {回声<h1>".$linha['titulo'] ."</h1>";echo " 发布者 " .$linha['username']."上 " .".$linha['data'] .</y>"."</i>";回声<p>";回声 $linha['texto'];$id = $linha['id_noticia'];如果(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> <?php}}?><脚本语言="javascript">函数 open_win_editar() {窗口.打开("noticiaEditarForm.php?id_noticia=<?php echo $id; ?>","编辑通知",位置=1,状态=1,滚动条=1,宽度=800,高度=455");}<?php mysql_close($conn);?>
我的观点是然后使用另一个查询来获取文章的标题和文本,以在所见即所得的编辑器上显示.
谁能指出我的缺陷?
这段代码:
发生在 PHP while
循环之外,因此 $id
的值将是设置为 $id
的最后一个值在循环.因此 JavaScript 代码将始终打开相同的链接.
如果您需要 PHP 循环中的代码为 JavaScript 指定 $id
值,那么您可以将其作为参数传递给 JavaScript 函数.像这样:
所以在循环中渲染锚标记的代码会像这样传递参数:
<a href="" onClick="open_win_editar(<?php echo $id; ?>)">Editar</a>
然后呈现的输出将在每个 a
标签上包含记录特定的 $id
值,供客户端上的 JavaScript 代码使用.
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>
And then a $_GET
on the other page.
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.)
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.
Can anyone point out my flaw?
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.
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:
<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.
这篇关于Javascript 和 PHP (window.open)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!