Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

passing variable to the same page after button submit

Status
Not open for further replies.

imad77

Instructor
Oct 18, 2008
97
CA
Hi,

I experience a strange issue in my Perl CGI script where I'm not able to keep the value of a variable after button submit.
When I run the script, it displays the upload to Folder1 by default(it's a requirement). I can change to Folder2 by clicking on a hyperlink with variable var(name of folder)
<a href="script.pl?var=$var">
I use this command to retrieve this value:
$var=query->param('var');

The problem:
First: I click on the Folder2's hyperlink. Second: I load a file from C:. Third: I click on button Submit to upload file to Linux server.
Result: the script uploads the file in Folder1 instead Folder2 and displays the page of Folder1 instead Folder2.
When I try to debug the script I noticed that the value of var is restted to "" after clicking on button submit.


Is it a way to keep the previous value after button submit in Perl?

Thanks a lot for your suggestions.
 
Not if its a file field:

<input type="file">

As far as I know you can't keep the previous value, or any value. Its probably a built in security feature of HTML forms.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
HI Kevin,

What do you mean by :

Not if its a file field:
<input type="file">

Thanks
 
I was guessing that you are trying to put a value back into a file field in a form using $var, which is not possible. Besides that guess, I don't understand the problem you are trying to describe.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
the $var contains only the name of the folder. Because I have 2 folders. My goal is to have a webpage for the first folder and a hyperlink for the second folder that I can switch to the folder2.
 
If you want to have your perl script change folders you use chdir() in the perl script. But I have a feeling I am still not understanding what it is you are trying to do.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I think I see what he's asking about...

1) Load the CGI first, with no params being sent to it at all. It defaults to showing "Folder1"

2) A link on the page links to "script.pl?var=Folder2" or some such. The CGI script reloads, this time with "var=Folder2" as a param, and shows the folder "Folder2"

3) On the Folder2 page, there's a file upload form. He submits the form, which sends "file=C:\test.txt" or some such to the server.

4) The CGI is reloaded, this time with params "file=C:\test.txt". Since the param "var" wasn't passed as before, it defaults back to "Folder1" since the variable needed to go to Folder2 wasn't passed.

So if you want "var=Folder2" to be passed after the second request (with your file upload), you need to include that field in your form again.

Code:
<form action="script.pl" method="post" enctype="multipart/form-data">
[COLOR=red]<input type="hidden" name="var" value="Folder2">[/color]
<input type="file" name="file">
<input type="submit">
</form>

So now when you hit upload, the params going to the script would be "var=Folder2&file=C:\test.txt" or w/e

Is this right?

-------------
Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Hi Kirsle,

Thanks a lot for suggestion, I tried another way but I used "hidden" param like this: I call this command before hitting Submit button.

print $q->hidden('var2',$var);

I call the following part of commands in the beginning of the subroutine where it displays the forms: And here I retrieve $var from the hidden var2.

if ($q->param('var2')) {
$var=$q->param('var2');
}else{
$q->param('var2',$var);
}


Very nice friend,

Thanks for everybody.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top