【问题标题】:Multiple window.open using JavaScript in php多个 window.open 在 php 中使用 JavaScript
【发布时间】:2014-12-06 17:53:24
【问题描述】:

我正在尝试使用 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>

";
}
?>

【问题讨论】:

  • 可能这是线程问题。尝试将.submit() 代码包装在window.setTimeout(..., 500) 中。
  • 如果您在两个表单上指定 target='TheWindow',两个表单都会尝试将输出发送到同一个命名窗口。要么完全省略 target,要么指定 target='_blank'(不要忘记下划线。)

标签: javascript php window.open


【解决方案1】:

请记住以下几点:

  1. 您对 openWindow() 函数的 2 次调用将创建 2 个相同的表单及其下方的 javascript 块。
  2. 由于您已将表单的 ID 硬编码为“TheForm”,因此这是非法的,因为元素 ID 在 DOM 中应该是唯一的。因此 document.getElementById('TheForm') 是模棱两可的
  3. 最好使用 JavaScript 打开 URL,例如使用 window.open 而不是 PHP 函数调用

示例: 由于需求的细节是未知的,因此很难提出正确的解决方案,即使您的初始代码可以正常工作。我不建议尝试以编程方式打开多个新选项卡。一个原因是可用性/用户体验。另一个原因是没有标准支持的方法可以在各种浏览器和浏览器设置上运行。建议在新标签页中打开 css3。

您可能需要考虑的 2 个解决方案: a) 打开多个页面作为弹出窗口 b) 为每个学生打开一个包含多条记录的页面 根据你的追求,你需要做出决定。在大多数情况下,选项 be 将是合适的。

选项(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");
?>

【讨论】:

  • 是的,你是对的,但是当我将表单 id 替换为 php 变量 $name 时,现在它只会打开空白标签。我在网上搜索,但不明白为什么每次都运行良好但不是我的。我也用户 alert() 那个aler也是空白的。请帮助我。
  • 你告诉我正确的方式是form id应该是不同的,谢谢
  • 不客气。是的,身份冲突是一个/主要问题。关于示例 php 脚本的注意事项:需要注意的是,不需要 test.php 中的表单,因为正在使用 window.open。如果必须 POST 而不是 GET,则应使用表单或 Ajax。但是,在这种情况下,人们会回到单窗口问题,并希望使用多个弹出窗口、每页多个学生信息或 div 来解决它。
【解决方案2】:

这是工作代码

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

   ";
}
?>

【讨论】:

    【解决方案3】:

    如果您在脚本中指定另一个调用 window.open() 方法,您应该让它工作:

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

    然后你可以给你想要的参数:window.open(url, name, specs, replace)

    希望对你有帮助。

    【讨论】:

    • 如问题所示,我发送了两个名为 name 和 id 的 POST,但在您的解决方案中,只有一个寡妇打开了发布的数据,但其他窗口没有 POST,只显示空白窗口。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多