2

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>

";
}
?>
2
  • May be this is a threading issue. Try wrapping the .submit() code inside window.setTimeout(..., 500). Commented Dec 6, 2014 at 18:49
  • If you specify target='TheWindow' on two forms, both forms will try to send output to the same named window. Either leave out target entirely or specify target='_blank' (and don't forget the underscore.) Commented Dec 8, 2014 at 21:01

3 Answers 3

1

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");
?>
Sign up to request clarification or add additional context in comments.

3 Comments

Yes you are right but when I replace form id to php variable $name then now it will open only blank tabes.I search over net but don't understand why every once work well but not mine. I also user alert(<?php echo $name?>) that aler is also blank. please help me on this.
You show me the currect way that is form id should be diffrent, thanks
You are welcome. Yes, id conflict is a/the major issue. Notes about sample php scripts: It should be noted that the form in test.php is not needed, since window.open is being used. If one must POST instead of GET, a form or Ajax should be used. However, in that case, one would fall back into the one-window problem and would want to use multiple popups, multiple student info per page, or divs to solve it.
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>

   ";
}
?>

Comments

0

if you specify another call of the window.open() method in your script, you should get it working:

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

I hope it'll help.

1 Comment

As shown in question I am sending two POST named as name and id, but in your solution only one widow is open with posted data but other windows are not with POST just blank windows are shown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.