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

时间:2019-04-09 04:25:27

标签: javascript php

我有PHP循环(WHILE {}),该循环从MySQL循环一个数组,并逐行创建一个包含数据的页面。我需要在每一行中添加一个按钮,该按钮将触发一个新页面,但是我需要将该行的ID值携带到该新的弹出页面中。这是我的代码,但是我不知道如何将我的ID传递给弹出窗口:

我知道我无法将PHP变量添加到JS函数的URL中,因为它位于循环之外。我可以使用JS函数添加它吗?这是一些简化的代码:

  

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

<script>
        var myWindow;

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

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

我需要将$ id转到新页面。如前所述,我希望将其作为$ _GET变量添加到URL中,但是由于url位于while循环之外的JS函数中,因此我似乎不知道如何添加它。

对不起,缩进

5 个答案:

答案 0 :(得分:4)

只需传递一个参数

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>

答案 1 :(得分:1)

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 ?>

和您的JS

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

我使用了HTML属性来存储值(而不是将其注入onclick表达式中),以避免必须同时用HTML和JS对值进行编码的问题。

我还使用了data-id而不是id,因为3位数表示HTML元素ID不好,容易发生冲突。

答案 2 :(得分:1)

希望它会为您工作。

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>

答案 3 :(得分:1)

要遵循的2个步骤

  1. 使用函数调用值引用将您的php变量 $ id 传递给javascript函数,
  2. 然后在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>
    

答案 4 :(得分:0)

如果仅在客户端使用cookie或HTML5 localStorage。

  

简单的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>
  

示例2简单的jQuery(通过函数作为URL中的查询字符串传递)

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

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>