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打开多个新窗口(浏览器选项卡)使用javascript但不知道为什么它将始终只打开一个窗口,也没有找到任何解决方案。这是我的代码。
<?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 个解决方案
#1
1
Keep the following in mind:
请记住以下几点:
- Your 2 calls to the openWindow() function will create 2 identical forms along with the javascript blocks below it.
- 你对openWindow()函数的2次调用将创建2个相同的表单以及它下面的javascript块。
- 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
- 由于您已将表单的ID硬编码为“TheForm”,因此它是非法的,因为元素ID在DOM中应该是唯一的。因此document.getElementById('TheForm')是不明确的
- You are better off using JavaScript to open URLs e.g. with window.open rather than the PHP function calls
- 您最好使用JavaScript打开网址,例如使用window.open而不是PHP函数调用
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.
示例:由于要求的详细信息未知,即使您的初始代码有效,也很难建议正确的解决方案。我不建议尝试以编程方式打开多个新选项卡。一个原因是可用性/用户体验。另一个原因是没有标准的支持方法可以在各种浏览器和浏览器设置上使用它。 css3建议在新标签中打开。
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.
您可能需要考虑的2个解决方案:a)打开多个页面作为弹出窗口b)为每个学生打开一个包含多个记录的单个页面根据您的目标,您需要做出决定。对于大多数情况,选项将是合适的。
Sample of option (a):
选项(a)的样本:
test.php
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
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
1
And here is working code
这是工作代码
<?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
0
if you specify another call of the window.open()
method in your script, you should get it working:
如果你在脚本中指定另一个window.open()方法的调用,你应该让它工作:
<script type='text/javascript'>
window.open('', 'TheWindow');
window.open();
document.getElementById('TheForm').submit();
</script>
You can then give the parameters you want: window.open(url, name, specs, replace)
然后,您可以提供所需的参数:window.open(url,name,specs,replace)
I hope it'll help.
我希望它会有所帮助。
#1
1
Keep the following in mind:
请记住以下几点:
- Your 2 calls to the openWindow() function will create 2 identical forms along with the javascript blocks below it.
- 你对openWindow()函数的2次调用将创建2个相同的表单以及它下面的javascript块。
- 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
- 由于您已将表单的ID硬编码为“TheForm”,因此它是非法的,因为元素ID在DOM中应该是唯一的。因此document.getElementById('TheForm')是不明确的
- You are better off using JavaScript to open URLs e.g. with window.open rather than the PHP function calls
- 您最好使用JavaScript打开网址,例如使用window.open而不是PHP函数调用
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.
示例:由于要求的详细信息未知,即使您的初始代码有效,也很难建议正确的解决方案。我不建议尝试以编程方式打开多个新选项卡。一个原因是可用性/用户体验。另一个原因是没有标准的支持方法可以在各种浏览器和浏览器设置上使用它。 css3建议在新标签中打开。
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.
您可能需要考虑的2个解决方案:a)打开多个页面作为弹出窗口b)为每个学生打开一个包含多个记录的单个页面根据您的目标,您需要做出决定。对于大多数情况,选项将是合适的。
Sample of option (a):
选项(a)的样本:
test.php
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
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
1
And here is working code
这是工作代码
<?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
0
if you specify another call of the window.open()
method in your script, you should get it working:
如果你在脚本中指定另一个window.open()方法的调用,你应该让它工作:
<script type='text/javascript'>
window.open('', 'TheWindow');
window.open();
document.getElementById('TheForm').submit();
</script>
You can then give the parameters you want: window.open(url, name, specs, replace)
然后,您可以提供所需的参数:window.open(url,name,specs,replace)
I hope it'll help.
我希望它会有所帮助。