...

View Full Version : Passing Var via Pop-up



soma56
12-28-2010, 08:38 PM
I've had some help figuring out how to pass a variable through a pop-up window:



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



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

Old Pedant
12-28-2010, 09:35 PM
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'.

Old Pedant
12-28-2010, 09:37 PM
I won't keep you in suspense.


$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;
}

Old Pedant
12-28-2010, 09:39 PM
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.

soma56
12-28-2010, 09:41 PM
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:



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

Old Pedant
12-28-2010, 09:49 PM
Okay, we'll ignore the PHP error. Or probably non-error.


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

soma56
12-28-2010, 10:01 PM
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.



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum