Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/407.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在window.open函数中传递php变量_Javascript_Php - Fatal编程技术网

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

Javascript 如何在window.open函数中传递php变量,javascript,php,Javascript,Php,我有一个PHP循环(WHILE{}),它从MySQL循环一个数组,并逐行创建一个包含数据的页面。我需要在每一行添加一个按钮,该按钮将触发一个新页面,但我需要将该行的id值带到新的弹出页面。这是我的代码,但我不知道如何将我的id传递到弹出窗口: 我知道我不能将PHP变量添加到JS函数中的URL,因为它位于循环之外。我可以使用JS函数添加它吗?下面是一些简化的代码: PHP代码 Javascript var-myWindow; 函数openWin(){ myWindow=窗口。打开(“htt

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

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

PHP代码



Javascript


var-myWindow;
函数openWin(){
myWindow=窗口。打开(“http://www.website.com/pop.php“我的窗户”,“宽度=600,高度=800”);
}
函数closeWin(){
myWindow.close();
}
我需要把$id转到新页面。如上所述,我希望将其作为$\u GET变量添加到URL中,但我似乎不知道如何添加它,因为URL位于while循环之外的JS函数中

很抱歉缩进不好

请传递一个参数

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>
while($all=mysql\u fetch\u数组($get\u all)){
$id=$all['id'];
“回声”点击
这里';
}
var-myWindow;
函数openWin(id){
myWindow=窗口。打开(“http://www.website.com/pop.php?p_id=“+id,“我的窗户”,“宽度=600,高度=800”);
}
函数closeWin(){
myWindow.close();
}

添加
id
作为HTML
元素的属性,并将其传递给
openWin
函数,例如

while($all=mysql\u fetch\u数组($get\u all)):?>

如果cookie或HTML5 localStorage纯粹在客户端,请使用它

简单jQuery(在URL中作为查询字符串传递)

本地存储

Ajax


$.ajax({
类型:“POST”,
url:package.php,
数据:$(“#myform”).serialize(),
成功:功能(数据)
{
$('#Result').html(数据);
}
});

希望它对您有用

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>
while($all=mysql\u fetch\u数组($get\u all)){
...
$id=$all['id'];
回声“点击这里”;
...
}
var-myWindow;
函数openWin(id){
myWindow=窗口。打开(“http://www.website.com/pop.php?id=“+id,“我的窗户”,“宽度=600,高度=800”);
}
函数closeWin(){
myWindow.close();
}
2个要遵循的步骤
  • 使用按值引用的函数调用将php变量$id传递给javascript函数
  • 然后在获取要在新窗口中打开的url的请求中使用该参数

    $id=$all['id'];
    回声“点击这里”;
    }
    ?>
    var-myWindow;
    函数openWin(id){
    myWindow=窗口。打开(“http://www.website.com/pop.php?id=“+id,“我的窗户”,“宽度=600,高度=800”);
    }
    函数closeWin(){
    myWindow.close();
    }
    

  • 您的
    echo
    中嵌套的单引号在while循环中无效,可能是多个id,因此您希望将多个id传递给URL或仅传递单个id?单个id..它是一个3位数的数字您可以上传屏幕快照我知道OP说id是一个三位数的数字,但您仍应注意XSS的漏洞,将其注入HTML和在
    window.open()中使用时
    
    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>
    
    <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>
    
    function set_url_data(go_to_url, data) {
      new_url = go_to_url + '?data=' + data;
      window.location.href = new_url;
    }
    
    //Set data
    
        localStorage.setItem('bookedStatus' + customerId, true);
    
        //Get data
    
        localStorage.getItem('bookedStatus');
    
    <script>
         $.ajax({
           type: "POST",
           url: package.php,
           data: $("#myform").serialize(), 
           success: function(data)
           {
               $('#Result').html(data); 
           }
        });
    </script>
    
    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>
    
    $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>