Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    43
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Passing Var via Pop-up

    I've had some help figuring out how to pass a variable through a pop-up window:

    Code:
    <script type="text/javascript">
    function expo(o) {
      var et = document.getElementById(o);
      if(et) {
        window.open( "exp.php?exportthis="+et.value, "myWindow", "status = 1, height = 300, width = 300, resizable = 0");
      }
    }
    </script>
    
    
    <form action="" method="post"><input type=hidden value="$file" name="exportthis" ><input type=submit value=Export name= "Exp"onclick="expo('exportthis');" />
    </form>
    However, my issue is that I have multiple forms. The forms are the same - the variables are different. The code above only works with the first variable returned. Here's an example of the problem.

    Code:
    <?PHP 
    
    //for example purposes the filename will be labeled as numbers
    $i = 1;
    
    while ($i < 5) {
    echo "<form action=\"\" method=\"post\"><input type=hidden value=\"$i\" name=\"exportthis\" ><input type=submit  value=\"Export\" name= \"Exp\"onclick=\"expo('exportthis');\" /></form>";
    }
    ?>
    The above simple PHP script produces 5 simple forms. All of them are identical with the exception of the 'exportthis' or variable that I'm trying to pass through. They are numbered '1' to '5'. If I press the submit button for any of them only the first variable (in this case '1') is passed through.

    Can someone help me modify the javascript so that the right variable is passed through and not just the first one?
    Last edited by soma56; 12-28-2010 at 08:47 PM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Ummm...this is a PHP question.
    All of them are identical with the exception of the 'exportthis' or variable that I'm trying to pass through. They are numbered '1' to '5'.
    Not at all true.

    With the code you show, they will all be 100% identical and will all have ONLY the value '1' for 'exportthis'.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    I won't keep you in suspense.
    Code:
    $i = 1;
    
    while ($i <= 5) {
    echo "<form action=\"\" method=\"post\"><input type=hidden value=\"$i\" name=\"exportthis\" ><input type=submit  value=\"Export\" name= \"Exp\"onclick=\"expo('exportthis');\" /></form>";
        ++$i;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Actually, come to think of it, with the missing ++$i in there, you have an infinite loop in your PHP code, so it can't possibly be working.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Jul 2008
    Posts
    43
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I won't keep you in suspense.
    $i = 1++;
    Right, I forgot to add that. Yes, this code/my problem contains php however the issue with respect to solving it revolves around javascript.

    For good measure I'll remove it completely from the code:

    Code:
    <script type="text/javascript">
    function expo(o) {
      var et = document.getElementById(o);
      if(et) {
        window.open( "exp.php?exportthis="+et.value, "myWindow", "status = 1, height = 300, width = 300, resizable = 0");
      }
    }
    </script>
    
    
    <form action="" method="post"><input type=hidden value="file-A" name="exportthis" >
    <input type=submit value=Export name= "Exp"onclick="expo('exportthis');" />
    </form>
    
    <form action="" method="post"><input type=hidden value="file-B" name="exportthis" >
    <input type=submit value=Export name= "Exp"onclick="expo('exportthis');" />
    </form>
    
    <form action="" method="post"><input type=hidden value="file-C" name="exportthis" >
    <input type=submit value=Export name= "Exp"onclick="expo('exportthis');" />
    </form>
    
    <form action="" method="post"><input type=hidden value="file-D" name="exportthis" >
    <input type=submit value=Export name= "Exp"onclick="expo('exportthis');" />
    </form>
    The script works but only returns the first form value. Can someone help me modify the script so that the corresponding/correct value is returned the form submitted?
    Last edited by soma56; 12-28-2010 at 09:44 PM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Okay, we'll ignore the PHP error. Or probably non-error.
    Code:
    <script type="text/javascript">
    function expo(field) 
    {
        window.open( "exp.php?exportthis="+field.value, 
                     "myWindow", 
                     "status = 1, height = 300, width = 300, resizable = 0");
      }
    }
    </script>
    
    <form action="" method="post">
        <input type=hidden value="$file" name="exportthis" >
        <input type=submit value=Export name="Exp" onclick="expo(this.form.exportthis);" />
    </form>
    Your problem was in trying to use document.getElementById. You have *FIVE* elements with the SAME ID. And the fact that you were picking up anything at all is a function of the browser you are using. First of all, no browser except MSIE would allow you to use getElementById() with the *NAME* of a form field. And other browsers won't give you any usable result when you use getElementById() with a multiply-defined ID. Only MSIE has the dual "feature" (if you can call it that) of (a) allowing you to use a name in place of an ID and (b) picking the FIRST object with the given id/name instead of barfing.

    By using a *relative* access as shown above, you first get a reference to the <form> that contains both the given submit and the given exportthis field ("this.form") and then, since you now have the guaranteed-correct form, you can get to the named field ("this.form.exportthis").

    And, yes, this works in all browsers.


    **********

    EDIT: LOL! Teach me to get ahead of myself. But the above should give you the right answer.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    soma56 (12-28-2010)

  • #7
    New Coder
    Join Date
    Jul 2008
    Posts
    43
    Thanks
    7
    Thanked 0 Times in 0 Posts
    That did it. Thank you for posting the logic behind it as well.

    I've cleaned up the code a little (I had two name tags) for any future users that need a sample for their own projects.

    Code:
    <head>
    
    <script type="text/javascript">
    
    function expo(field) 
    {
        window.open( "exp.php?exportthis="+field.value, 
                     "myWindow", 
                     "status = 1, height = 300, width = 300, resizable = 0");
      }
    
    </script>
    
    
    </head>
    
    <body>
    <form action="" method="post">
        <input type=hidden value="file-A" name="exportthis" >
        <input type=submit value=Export onclick="expo(this.form.exportthis);" />
    </form>
    
    <form action="" method="post">
        <input type=hidden value="file-B" name="exportthis" >
        <input type=submit value=Export onclick="expo(this.form.exportthis);" />
    </form>
    
    <form action="" method="post">
        <input type=hidden value="file-C" name="exportthis" >
        <input type=submit value=Export onclick="expo(this.form.exportthis);" />
    </form>
    
    </body>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •