简体   繁体   English

使用 PHP 变量并将数据传递给 window.open 使用 javascript

[英]Using PHP variable and passing Data to window.open using javascript

I have this javascript function我有这个 javascript function

This code opens page in a new window此代码在新的 window 中打开页面

<script language="javascript" type="text/javascript">
    function OpenPopupCenter(pageURL, title, w, h) {
        var left = (screen.width - w) / 2;
        var top = (screen.height - h) / 4;  // for 25% - devide by 4  |  for 33% - devide by 3
        var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
    } 
</script>

Now I have this code to get the Id from a MySQL Database which looks like this:现在我有这段代码可以从 MySQL 数据库中获取 ID,如下所示:

<?php
require_once('inc/config.php');

$con = mysqli_connect($host,$user,$pass,$db) or die ('Cannot connect: '.mysqli_error());

$query = "SELECT * FROM new_reservation";
$result = mysqli_query($con,$query) or die('Bad Query: '.mysqli_error($con));
while($row = mysqli_fetch_array($result)){

$ResID = $row['id'];

}
?>

Now I want to use this same ID in the javascript function I created like so现在我想在我这样创建的 javascript function 中使用相同的 ID

onclick=\"OpenPopupCenter('signout_visitor.php?id=', 'TEST!?', 1200, 600);\"

I am not quite sure how to use this.我不太确定如何使用它。 Quite a bit new to using variables in popup windows for javascript hence i would be needing some form of clarification here.在弹出窗口 windows for javascript 中使用变量相当新,因此我需要在这里进行某种形式的澄清。

Edit编辑

I am using it in a Table like this我在这样的表中使用它

<?php
require_once('inc/config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot connect, Reason: '.mysqli_error());
$sql = "select * from new_reservation ";
$result = mysqli_query($con,$sql) or die ('Failed Query , Reason : '.mysqli_error($con));
while($row = mysqli_fetch_array($result)){

    $showData = "<TR valign=top>
<TD width=106 height=26><div class=wpmd>
<div align=center><font face=Verdana size=1><BR></font></div>
<div align=center><font face=Verdana size=1>&nbsp;&nbsp; {$row['visit_date']}</font></div>
</div>
</TD>
<TD width=134 height=26><div class=wpmd>
<div align=center><font face=Verdana size=1><BR></font></div>
<div align=center><font face=Verdana size=1>&nbsp; {$row['login_time']}</font></div>
</div>
</TD>
<TD width=148 height=26><div class=wpmd>
<div><font face=Verdana size=1><BR></font></div>
<div><font face=Verdana size=1>&nbsp;&nbsp;&nbsp; {$row['fullname']}</font></div>
</div>
</TD>
<TD width=160 height=26><div class=wpmd>
<div align=center><font face=Verdana size=1><BR></font></div>
<div align=center><font face=Verdana size=1>&nbsp;&nbsp; {$row['whom_tosee']}</font></div>
</div>
</TD>
<TD width=138 height=26><div class=wpmd>
<div>&nbsp;</div>
<div align=center><font face=Verdana><input type=image src=images/show_detailsbtn.png width=124 height=26 onclick=\"OpenPopupCenter('shownewvisitdetails.php?id=$ResID', 'TEST!?', 1200, 600);\" ></font></div>
</div>
</TD>
<TD width=163 height=26><div class=wpmd>
<div><BR></div>
<div>&nbsp;<font face=Verdana size=2>
    <input type=image src=images/signout_visitor.png width=121 height=27 onclick=\"OpenPopupCenter('signout_visitor.php?id=$ResID', 'TEST!?', 1200, 600);\"></font></div>
</div>
</TD>
</TR>
";
    echo $showData;

}
?>

Actually your question is a little bit confusing to me.其实你的问题让我有点困惑。 Can you be clear about what you are trying to achieve?你能清楚你想要达到的目标吗?

1) Do you need the URL params (i mean id) from URL you can use 1)您是否需要来自 URL 的 URL 参数(我的意思是 id),您可以使用

$_SERVER['REQUEST_URI']

in PHP and在 PHP 和

window.location

in JS.在 JS 中。

2) if you want to pass the result data to the newly opened window try this 2)如果要将结果数据传递给新打开的 window 试试这个

let windowNew = window.open('https://www.yahiya.com');

windowNew.array= $dataArray;

and in your new window use并在您的新 window 中使用

windowNew.array;

If you want to use ID from PHP in your javascript, just after php code simply use this:如果您想在 javascript 中使用来自 PHP 的 ID,只需在 php 代码之后使用:

onclick=\"OpenPopupCenter('signout_visitor.php?id=<?php echo $ResID;?>', 'TEST!?', 1200, 600);\"

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

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