我如何通过PHP GET URL变量用Javascript打开一个窗口

How can I pass PHP GET URL variables to open a window with Javascript?

本文关键字:窗口 一个 Javascript PHP 何通过 GET URL 变量      更新时间:2023-09-26

我想通过一个PHP get变量从链接到Javascript,所以我可以打开一个新的较小的窗口与适当的内容从值传递到URL ..我试着在下面这样做,但我做不到……非常感谢你的帮助。下面的代码生成图像超链接,每个超链接都有来自数据库的ID,因此当单击图像时,应该打开一个新窗口,但ID应该传递给javascript窗口。开放方法…我试图用AJAX根据get变量加载内容,但我做不到!

<?php
require('../database/connect.php');
database_connect();
$query = "select * from Entertainers";
$result = $connection->query($query);
$row_count =$result->num_rows;
for($i = 1; $i <= $row_count; $i++)
  {
   $row = $result->fetch_assoc();

?>
<?php  echo "<a href='' onclick='window.open(profile.php?id=".$row['ID'].")'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; } ?>

不要忘记在Javascript open函数中引用url。另外,您是否考虑过使用printf()进行输出?

$link =
'<a href="" onclick="window.open(''profile.php?id=%d'')">'
. '<img src="../%s" width="100" height="100" /></a>' . PHP_EOL;
for($i = 1; $i <= $row_count; $i++) {
    $row = $result->fetch_assoc();
    printf($link,$row['ID'],$row['Picture']);
}

%d表示小数,%s表示上述字符串中的字符串(因此是$link)。另一个提示:如果你没有特别的理由使用for循环,使用while循环会使你的代码更简洁。

while ($row = $result->fetch_assoc()) {
    printf($link,$row['ID'],$row['Picture']);
}

当您的脚本在页面上呈现HTML时,window.openURL参数是而不是被呈现为string

您的代码当前在页面上呈现的内容:

<a href='' onclick='window.open(profile.php?id={some_id})'><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; } ?>

URL profile.php?id={some_id}在客户端被解析时是而不是字符串。

试试这个:

<?php  echo "<a href='' onclick='"window.open('profile.php?id=" . $row['ID'] . "');'"><img src ='../".$row['Picture']."' width='100' height='100' /> </a>"; ?>