doutidi5037 2014-12-06 17:53
浏览 77
已采纳

多个window.open在php中使用JavaScript

I am trying to open multiple new windows(browser tabs) from php using javascript but don't know why it will open only one window always, and also don't found any solution of this. Here is my code.

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

";
}
?>
  • 写回答

3条回答 默认 最新

  • douzhi4991 2014-12-06 18:47
    关注

    Keep the following in mind:

    1. Your 2 calls to the openWindow() function will create 2 identical forms along with the javascript blocks below it.
    2. Since you have hardcoded the id of the form to be 'TheForm', it is illegal, since the element id should be unique in the DOM. Hence document.getElementById('TheForm') is ambiguous
    3. You are better off using JavaScript to open URLs e.g. with window.open rather than the PHP function calls

    Example: Since the details of the requirements are unknown, it is difficult to suggest the correct solution, even if you get your initial code working. I would not recommend trying to open multiple new tabs programmatically. One reason is usability/user experience. Another reason is that there is no standard supported method to get this working on a variety of browsers and browser settings. There is css3 recommendation for opening in a new tab.

    2 solutions you might want to consider: a) Opening multiple pages as popups b) Opening a single page with multiple records for each student Depending on what you are after, you need to decide. For most cases, option be will be suitable.

    Sample of option (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>
    

    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");
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog