Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.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
在php中使用JavaScript打开多个窗口_Javascript_Php_Window.open - Fatal编程技术网

在php中使用JavaScript打开多个窗口

在php中使用JavaScript打开多个窗口,javascript,php,window.open,Javascript,Php,Window.open,我正在尝试使用javascript从php打开多个新窗口(浏览器选项卡),但不知道为什么它总是只打开一个窗口,也没有找到任何解决方案。这是我的密码 <?php openWindow("name1","id1"); openWindow("name2","id2"); function openWindow($name,$id) { echo "name = $name and Id = $id"; echo " <form id=$name method='post' act

我正在尝试使用javascript从php打开多个新窗口(浏览器选项卡),但不知道为什么它总是只打开一个窗口,也没有找到任何解决方案。这是我的密码

<?php

openWindow("name1","id1");
openWindow("name2","id2");

function openWindow($name,$id)
{
echo "name = $name and Id = $id";

echo "

<form id=$name method='post' action='studentDetails.php' target='TheWindow'>
<input type='hidden' name='name' value=$name />
<input type='hidden' name='id' value=$id />
</form>

<script type='text/javascript'>
window.open('', 'TheWindow');
document.getElementById(<?php echo $name;?>).submit();
</script>

";
}
?>

如果在脚本中指定另一个调用
window.open()
方法,则应使其正常工作:

<script type='text/javascript'>
  window.open('', 'TheWindow');
  window.open();
  document.getElementById('TheForm').submit();
</script>

窗口。打开(“窗口”);
window.open();
document.getElementById('TheForm').submit();
然后,您可以提供所需的参数:
window.open(url、名称、规格、替换)


我希望这会有所帮助。

记住以下几点:

  • 对openWindow()函数的两次调用将创建两个相同的表单及其下面的javascript块
  • 由于您已将表单的id硬编码为“TheForm”,这是非法的,因为元素id在DOM中应该是唯一的。因此document.getElementById(“TheForm”)是不明确的
  • 最好使用JavaScript打开URL,例如使用window.open,而不是使用PHP函数调用
  • 例如: 由于需求的细节未知,因此很难提出正确的解决方案,即使您的初始代码正常工作。我不建议尝试以编程方式打开多个新选项卡。一个原因是可用性/用户体验。另一个原因是,没有标准支持的方法可以在各种浏览器和浏览器设置上工作。css3建议在新选项卡中打开

    您可能需要考虑的两种解决方案: a) 以弹出窗口形式打开多个页面 b) 为每个学生打开包含多个记录的单个页面 根据你的目标,你需要做出决定。对于大多数情况,选项将是合适的

    选项(a)的示例:

    test.php

    <html>
        <head>
        </head>
        <body>
            <form id='studentform' method='post' action='studentDetails.php' target="_blank">
            <input type='hidden' name='name' id="name" value="" />
            <input type='hidden' name='id' id="id" value="" />
            </form>
    
            <script type='text/javascript'>
                function openWindows(){
                    window.open("http://localhost/test/studentDetails/studentDetails.php?name=john&id=1", "", "width=600, height=300");
                    window.open("http://localhost/test/studentDetails/studentDetails.php?name=mary&id=2", "", "width=600, height=300");
                }
    
                openWindows();
    
            </script>
    
        </body>
    </html>
    
    
    函数openWindows(){
    窗口打开(“http://localhost/test/studentDetails/studentDetails.php?name=john&id=1“宽度=600,高度=300”);
    窗口打开(“http://localhost/test/studentDetails/studentDetails.php?name=mary&id=2“宽度=600,高度=300”);
    }
    openWindows();
    
    studentDetails.php

    <?php
        function getParam($name,$method){
            if($method=="POST")
                return isset($_POST[$name])?$_POST[$name]:"";
            else    
                return isset($_GET[$name])?$_GET[$name]:"";
        }
        $name = getParam("name", "GET");
        $id = getParam("id", "GET");
        if($name && $id)
            echo("name = $name and Id = $id");
        else
            echo("Invalid student info");
    ?>
    

    下面是工作代码

    <?php
    openWindow("name1","Id1");
    openWindow("name2","Id2");
    
    
    function openWindow($name,$studentId)
    {
        echo "
    
        <form id='$name' method='post' action='studentDetails.php' target='_blank'>
        <input type='hidden' name='name' value=$name />
        <input type='hidden' name='studentId' value=$studentId />
        </form>
    
        <script type='text/javascript'>
        document.getElementById('$name').submit();
        </script>
    
       ";
    }
    ?>
    

    如问题中所示,我正在发送两篇名为name和id的文章,但在您的解决方案中,只有一个窗口打开了发布的数据,而其他窗口没有发布,只是显示了空白窗口。这可能是线程问题。尝试将
    .submit()
    代码包装到
    窗口中。设置超时(…,500)
    。如果在两个窗体上指定target='TheWindow',则两个窗体都将尝试将输出发送到同一命名窗口。要么完全省略target,要么指定target=''u blank'(不要忘记下划线)。是的,你是对的,但当我将表单id替换为php变量$name时,现在它将只打开空白选项卡。我在网上搜索,但不明白为什么每一次都能很好地工作,但我的却不行。我还提醒用户aler也为空。请在这方面帮助我。您告诉我当前的方式,即表单id应该是不同的,谢谢您的欢迎。是的,身份冲突是主要问题。关于示例php脚本的注释:应该注意,test.php中的表单是不需要的,因为正在使用window.open。如果必须使用POST而不是GET,则应使用表单或Ajax。然而,在这种情况下,人们会退回到一个窗口的问题,并希望使用多个弹出窗口、每页多个学生信息或div来解决这个问题。