简体   繁体   English

如何在window.open函数中传递php变量

[英]How to pass a php variable in window.open function

I have PHP loop ( WHILE{} ) that loops an array from MySQL and creates a page with the data line by line. 我有PHP循环(WHILE {}),它从MySQL循环一个数组,并逐行创建一个包含数据的页面。 I need to add to each line a button that will trigger a new page but I need to carry the value of the id of the line to that new pop-up page. 我需要在每一行添加一个触发新页面的按钮,但我需要将该行的id值带到新的弹出页面。 This is the code I have, but i can't figure out how to pass my id to the pop up: 这是我的代码,但我无法弄清楚如何将我的id传递给弹出窗口:

I know I can't add my PHP variable to the URL in the JS function, because it sits outside the loop. 我知道我不能将我的PHP变量添加到JS函数中的URL,因为它位于循环之外。 Can I use the JS function to add it ? 我可以使用JS函数添加它吗? Here is some simplified code: 这是一些简化的代码:

PHP Code PHP代码

<?php
    while ($all = mysql_fetch_array($get_all)){

        $id= $all['id'];

        echo '<button type='button' class='btn btn-danger' onclick='openWin()'>  Click here</button> ';

    }
?>

Javascript 使用Javascript

<script>
        var myWindow;

        function openWin() {
            myWindow = window.open("http://www.website.com/pop.php", "myWindow", "width=600,height=800");
        }

        function closeWin() {
            myWindow.close();
        }
</script>

I need to get $id to the new page. 我需要将$ id添加到新页面。 As stated I was hoping to add it to the URL as a $_GET variable, but I don't seem to know how to add it since the url sits in the JS function outside the while loop. 如上所述,我希望将它作为$ _GET变量添加到URL中,但我似乎不知道如何添加它,因为url位于while循环之外的JS函数中。

Sorry for the poor indenting 抱歉可怜的缩进

just pass a parameter 只是传递一个参数

while ($all = mysql_fetch_array($get_all)){

 $id= $all['id'];
 echo '<button type="button" class="btn btn-danger" onclick="openWin(\''.$id.'\')">  Click 
   here</button> ';
}



<script>
        var myWindow;
        function openWin(id) {
            myWindow = window.open("http://www.website.com/pop.php?p_id="+id, "myWindow", "width=600,height=800");
        }
        function closeWin() {
            myWindow.close();
        }
</script>

Add the id as an attribute of your HTML <button> element and pass that to the openWin function, eg 添加id作为HTML <button>元素的属性,并将其传递给openWin函数,例如

while ($all = mysql_fetch_array($get_all)) : ?>
<button type="button" class="btn btn-danger" onclick="openWin(this.dataset.id)"
        data-id="<?= htmlspecialchars($all['id']) ?>">
  Click here
</button>
<?php endwhile ?>

and in your JS 在你的JS中

function openWin(id) {
  myWindow = window.open(
      `http://www.website.com/pop.php?id=${encodeURIComponent(id)}`,
      'myWindow',
      'width=600,height=800'
  )
}

I've used an HTML attribute to store the value (as opposed to just injecting it into the onclick expression) to avoid the issue of having to simultaneously HTML and JS encode the value. 我已经使用HTML属性来存储值(而不是将其注入onclick表达式),以避免同时HTML和JS编码值的问题。

I've also used data-id instead of just id because a 3-digit number makes a bad HTML element ID, prone to easy conflicts. 我还使用了data-id而不仅仅是id因为3位数字会产生错误的HTML元素ID,容易发生冲突。

hope it will work for you. 希望它能为你效劳。

while ($all = mysql_fetch_array($get_all)){
    ...
    $id= $all['id'];
    echo "<button type='button' class='btn btn-danger' onclick='openWin($id)'>  Click here</button>";
    ...
    }

    <script>
            var myWindow;
            function openWin(id) {
                myWindow = window.open("http://www.website.com/pop.php?id="+id, "myWindow", "width=600,height=800");
            }
            function closeWin() {
                myWindow.close();
            }
    </script>

2 steps to follow 要遵循的2个步骤

  1. Pass your php variable $id to javascript function using function-call-by-value reference, 使用函数call-by-value引用将php变量$ id传递给javascript函数,
  2. then use that argument in GET request for url to be opened in new window 然后在GET请求中使用该参数在新窗口中打开url

     $id= $all['id']; echo "<button type='button' class='btn btn-danger' onclick='openWin(".$id.")'> Click here</button> "; } ?> <script> var myWindow; function openWin(id) { myWindow = window.open("http://www.website.com/pop.php?id="+id, "myWindow", "width=600,height=800"); } function closeWin() { myWindow.close(); } </script> 

Use cookies or HTML5 localStorage if its purely on the client-side. 如果纯粹在客户端使用cookie或HTML5 localStorage。

Simple jQuery (Pass as a query string in the URL) 简单的jQuery(在URL中作为查询字符串传递)

<script>
        var myWindow;
        function openWin(id) {
            myWindow = window.open("http://www.website.com/pop.php?p_id="+id, "myWindow", "width=600,height=800");
        }
        function closeWin() {
            myWindow.close();
        }
</script>

Example 2 Simple jQuery (Pass as a query string in the URL via function) 示例2简单的jQuery(通过函数传递为URL中的查询字符串)

function set_url_data(go_to_url, data) {
  new_url = go_to_url + '?data=' + data;
  window.location.href = new_url;
}

LocalStorage localStorage的

//Set data

    localStorage.setItem('bookedStatus' + customerId, true);

    //Get data

    localStorage.getItem('bookedStatus');

Ajax 阿贾克斯

<script>
     $.ajax({
       type: "POST",
       url: package.php,
       data: $("#myform").serialize(), 
       success: function(data)
       {
           $('#Result').html(data); 
       }
    });
</script>

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

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