LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2003, 04:01 PM   #1
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Handling multiple forms with same name hidden fields


I've been working on this cgi script and encountered a problem.

Basically, i have a script (Perl CGI) that prints a page with 2 forms both with a submit button and having each a hidden field with the same name, but different value.

The problem is that when ever i press either submit button the hidden field (because of the same name) get's overwritten by what ever hidden field was submitted to the script in the URL (get method).

Rough example script:

Code:
#! /usr/bin/perl

use CGI::Pretty ":standard";

print 
          header,
          start_html,
          start_form({ -method=>"get"}),
               hidden({ -name=>"h", value=>"1"}),
               submit,
          end_form,
          start_form({ -method=>"get"}),
               hidden({ -name=>"h", value=>"2"}),
               submit,
          end_form,
          end_html;
I tried to name the forms the same and such, but just can't seem to be able to avoid the problem.

Any suggestions?
 
Old 09-03-2003, 04:07 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Specify your script name as the action for the form. This will overwrite the default action which will include the query string the script was called with. I'm not used to using the CGI library htat much so post the actual html output (that is the important bit) If what I said is wrong or doesn;t make sense.
 
Old 09-03-2003, 04:23 PM   #3
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Original Poster
Rep: Reputation: 30
Better yet, here is the above script in action.

I've also added the script name as the action for the forms. Notice how "h" get's overwritten as soon as you press either button.
 
Old 09-03-2003, 04:51 PM   #4
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Original Poster
Rep: Reputation: 30
I think i've come up with a decent solution:
name the hidden fields in a sequential manner (so they all have a different name), then to extract the values, loop, untill you find a submitted param.

So if i have 2 hidden fields "h1" and "h2", etc... then to find that h2 was actually submitted i would loop through with param("h" . $count) untill i find something (with $count incrementing each time).

Well, seems to work, so i'm happy
 
Old 09-04-2003, 12:39 PM   #5
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Sorry I misunderstood what you said.

If you want to preserve it you can add an "-override=>1" value to the tag This stops it taking the value that was submitted.
 
Old 09-04-2003, 01:34 PM   #6
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Original Poster
Rep: Reputation: 30
Man, i completely forgot about that attribute!

Hehe
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing form values between multiple forms!! AskMe Programming 5 09-07-2005 07:44 PM
Postfix/Procmail multiple recipent handling problem kirkland500 Linux - Networking 2 03-15-2005 05:14 PM
Qmail handling multiple domains? Gsee *BSD 43 03-03-2005 06:46 AM
Multiple Forms in Qt Application 00it45 Programming 9 02-17-2004 01:41 AM
Joining multiple lines and summing fields elconde Programming 1 02-13-2004 10:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration