Solved

How to pass variables to a page from a form and perl/cgi at the same time

Posted on 2003-11-04
6
1,526 Views
Last Modified: 2013-12-25
I have a page that's dynamically generated by cgi and I pass variables to the next page with a link like this

generate_next_page.cgi?val1=$value1&val2=$value2

where $value1 and $value2 are variables within my cgi page.

The problem is that on one page I also have a form that gathers new variables and I want to pass my original variables ($value1 and $value 2) as well as the new variables that are picked up from the form.

I've tried this a few different ways and I either get my old variables in the url, or the new ones, but never both at the same time.   Any ideas?

Thanks

 
0
Comment
Question by:rezachar
6 Comments
 
LVL 5

Accepted Solution

by:
djplaistow earned 84 total points
ID: 9679521
The variables can not have the same names. The variable name must uniquely identify the variable.

I am not one hundred persent sure that I understand your question, so part of this is a guess. Instead of using a link to maintain the old variable's value, use hidden variables within the form itself. The perl might look like this:

print <<END_OF_TEXT;
 <form name=yourForm method=post action="generate_next_page.cgi">

  <input type=hidden name=val1 value="$value1">
  <input type=hidden name=val2 value="$value2">
  New value 1:<input type=text name=newValue1 size=18>
  New value 2:<input type=text name=newValue2 size=18>
  <input type=submit name=pageSubmit>
 </form>
END_OF_TEXT

Now when the user selects the submit button, the old values come in as val1 and val2. The new values come to the script as newValue1 and newValue2. Of course if your page looks better with a link for the submit than a button, just  change the href's url to explicitly execute the JavaScript submit() method on the form. Incidently, the submit() method is the reason you should never name a submit button "submit" as is all too common.
0
 
LVL 8

Assisted Solution

by:inq123
inq123 earned 83 total points
ID: 9683709
Hi rezachar,

I think dj's solution basically solves most of your problem.  You just need to change the "post" method in the form to "get" if you want to have the variables displayed on the URL OR if you only handle GET variables.

As for JS, just use:

print "<a href=\"javascript:document.yourForm.submit()\">next page</a>\n";

in your program to submit the form above.

Cheers!
0
 

Assisted Solution

by:elsamman
elsamman earned 83 total points
ID: 9701350
The solutions mentioned above will work.  I assume what you are trying to do, however, is to pass your old variables in as parameters on the action URL to save space. I could never get that to work.  I have done this by passing them as extra parameters rather than variables and then parsing them.  

Form syntax:

<form action=foo.pl/parm1=value1/param2=value2>

I then just have a generic bit of Perl to stuff them back into CGI parameters

      my $path_info = $query->path_info;
      $path_info .= '/';
      $path_info =~ s/\+/ /;
      $path_info =~ s/[^\/A-Za-z\-_ 0-9=]//g;
      while ($path_info =~ /\/([A-Za-z\-_0-9]*)=(.*?)\//) {
            $query->param($1, $2);
            $path_info =~ s/\/$1=$2//;
      }

You may have to tweak this depending on the values you expect (I don't really unescape things properly in this example).
0
 

Expert Comment

by:Teh_Craze
ID: 9939734
Could allways try persistant data via cookies, and CGI::Session
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Building Windows 10 1 84
inactive users 13 82
User profile Size Report 3 83
Renaming with batch file 4 39
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question