Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

multiple form interaction in cgi

by vdubjunkie (Novice)
on Nov 29, 2007 at 16:11 UTC ( [id://653886]=perlquestion: print w/replies, xml ) Need Help??

vdubjunkie has asked for the wisdom of the Perl Monks concerning the following question:

All,

This may be more of a HTML question, but it involves using CGI, and I imagine people here will know. What I am trying to do is to have multiple forms of various types which are able to affect one another. What I mean is that if a user selects an option in a popup over here, the options over there will be based upon that selection.

Ultimately these values will be populated by fields in a database, but I will worry about integrating that separately.

It seems impossible to avoid the idea that the page will actually be "redrawn" with each "submit," and I always seem to lose functionality at this point.

I have chopped out parts of a script I was trying to do something similar with, so understand that this code is "chopped"

What I am trying to do here is execute a system script when the user clicks the "Gather site info" button, and have the button change into a "Update site info" button since the initial gathering has already taken place. However, when you click the button, none of the buttons are redrawn. I have done this code a NUMBER of different ways, but I cannot seem to have this basic element of allowing events from one "button" affect other values and still have all other forms on the page stay in tact.

if ($gathered) {print"-!$gathered!-<br>"} #Print forms for displaying Site options if ($import{'site_info'}) { $site = $query->param('site'); $cc = $query->param('country_code'); $query->import_names('F'); $cc = uc($cc); print qq(<br>); print qq(<hr><center>); print $query->start_form; if (!$import{'Gather'}) { print $query->submit('Gather','Gather site info'); } elsif ($import{'Gather'}) { print $query->submit('Update','Update site info'); } print $query->endform; print "<hr>"; print $query->h3("Gather or Update<br>for tools below"); print "<hr>"; print $query->start_form(-method=>'post',-action=>'ap_chan.cgi', -target=>'main'); &hide; print $query->submit('chan_power','Chan/Power'); print $query->endform; print $query->start_form(-method=>'post',-action=>'world_mode.cgi' +, -target=>'main'); &hide; print $query->submit('world_mode','Set World Mode'); print $query->endform; print "<hr>"; print qq(</center>); } print qq(</body>\n</html>); sub hide { while(my($k,$v) = each(%import)) { print $query->hidden($k,$v); } print qq(<INPUT TYPE="hidden" NAME="isp" VALUE="$isp">\n); }
I implore the wisdom of the monks on this one.

Replies are listed 'Best First'.
Re: multiple form interaction in cgi
by snopal (Pilgrim) on Nov 29, 2007 at 16:39 UTC

    You can only submit one form at a time. Breaking up your document into separate forms will only mean that the much of the data you wish to manage will not be transferred to your CGI processor. A submit button works from within a specific form, and only the key/value pairs of that form are transmitted when that submit button is clicked.

    If you are trying to create one big dynamic form where components change based upon the values found on other parts of the page, that's DHTML (Javascript). That's an adventure with a variety of successes and pitfalls of its own.

Re: multiple form interaction in cgi
by philcrow (Priest) on Nov 29, 2007 at 16:45 UTC
    It sounds like you want AJAX. I recommend the book AJAX in Action (isbn: 1932394613).

    Phil

    The Gantry Web Framework Book is now available.

      Not to move off-topic here, but the original request was for managing a specific page of clicks and entries which are used to modify other items on the page. There is no need or call here for repeated calls to the server to fill out content.

      AJAX, in standard terminology is the behavior of altering content though hidden back-end communications between the client and the server.

      This user is requesting help on managing a single page with a simple rule-set. The solution might be extended to an AJAX type solution, but a properly loaded page will have all the rules provided in a single page hit to satisfy this design. The complexity of AJAX is not warranted here. Merely using simple Dynamic HTML with Javascript will satisfy the form rules as outlined simply and without the overhead of additional client/server communications.

      Since you provided a reference resource... I'll include my own:

      • _JavaScript - The Definitive Guide_ 5th ed, O'Reilly, isbn-10: 0-596-10199-2
      • _JavaScript & DHTML Cookbook_, O'Reilly, isbn-10: 0-596-00467-2
Re: multiple form interaction in cgi
by leocharre (Priest) on Nov 29, 2007 at 17:01 UTC
    Yah man... this is sounding like javascript, no doubt. *NOT* multiple forms.. but a form that customizes itself. What perl may be able to do for you here is write the javascript for you, predetermine some array values to put in the script etc. Unless you want the interface to be step by step.. you need javascript.

    If you're at a loss.. lookup javascript DOM, targetting objects via javascript with document write , setting ids on elements, etc. Getting this right may be a pain in the rear - and it will surely break cross-browser. Test it on various ones.

Re: multiple form interaction in cgi
by sundialsvc4 (Abbot) on Nov 29, 2007 at 19:03 UTC

    First of all, I have two (three...) very important things to say to you:

    • CGI::Application (or Catalyst)
    • Template Toolkit

    After you have visited http://search.cpan.org and spent a day or so getting your eyes opened by an introduction to these three modules, we can consider the ways that you can accomplish your task.

    If you use a regular CGI-application, then each time you submit a form, the server must send back the HTML for the new page to be displayed, and you incorporate the previously POSTed information into that.

    If, on the other hand, you use AJAX, you can update what the user sees ... by executing JavaScript code right there on the user's machine, without bothering the web server at all.

    Lest your eyes glaze-over at the thought of AJAX, any more than it might already be doing at the thought of using CGI without the tools I have just mentioned, know this ... “the tools that I have just mentioned” provide a very sophisticated and robust level of support for AJAX with a minimum of coding-effort on your part. In the Perl environment, thanks to CPAN, nearly everything that you might wish to do (except, of course, for what you wish to do... ;-) ...) has been advanced to a very, very high degree of sophistication. (On the JavaScript side, the Prototype JavaScript-library provides you “all the goodies of Ruby on Rails without the gemstones and locomotives.”) Instead of writing it, you just use it. Woot! Woot!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://653886]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-25 19:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found