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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now