Code Comments
Programming Forum and web based access to our favorite programming groups.Hi folks, I'm trying to create an HTML form that will refresh itself based on user input. In the POD for CGI it says that you can set the value of a named parameter by using something like: $query->param(-name=>'foo', -value=>'the value'); But it doesn't say (or at least I didn't see) anything about how to redisplay the form with the new value in the textbox (or a different textbox for that matter). The sample script at the top of the doco (perldoc CGI) creates a blank form that takes some input, a name, a "combination", and a color and displays the user input in the bottom half of the page. I would like to edit the value entered into the "name" textbox and display my edited value in the same, or possibly a different textbox. I've tried a bunch of things including the call to param() above, without success. For example, if the user types "Wilma" into the textbox, the script should populate the target textbox with my edited value, say, "Betty". As always, Thanks in Advance for any help! richf
Post Follow-up to this messageRichard Fernandez wrote: > I'm trying to create an HTML form that will refresh itself based on user > input. In the POD for CGI it says that you can set the value of a named > parameter by using something like: > > $query->param(-name=>'foo', -value=>'the value'); > > But it doesn't say (or at least I didn't see) anything about how to > redisplay the form with the new value in the textbox (or a different > textbox for that matter). If you are using the CGI methods to display the form, and (of course) you have the code to display the form _after_ the code that alters the values then is should just happen. > As always, Thanks in Advance for any help! As always, please reduce your problem to a minmum but complete script (that uses strict and warnings) and post it here in it's entirity, unaltered.
Post Follow-up to this message> $query->param(-name=3D>'foo', -value=3D>'the value');
Make that line $query->param(-name=3D>'foo', -value=3D>"$the_value");
Then somwhere at the start where you get the variables;
my $name =3D $query->param('name')||'';
if ($name =3D~/mary/i){ $the_value =3D "Howdy Mary" }
How you do it depends on your workflow/logic
Owen
Thanks for your response, but I still don't get it. I type my name into
the box and click=20
Submit, but it doesn't change!
Could you please clarify?
Here's my code:
#!/usr/bin/perl -w
# This script is the example at the top of the CGI pod --richf;
use strict;
use CGI;
my $html =3D new CGI;
my $new_value;
my $name =3D $html -> param('name') or '';
if ($name =3D~ /richf/i) {
$new_value =3D 'John';
}
print $html -> header,
$html -> start_html('A Simple Example'),
$html -> h1('A Simple Example'),
$html -> start_form,
"What's your name?",
$html -> textfield(-name =3D> 'name'),
$html -> p,
"What's the combination?",
$html -> p,
$html -> checkbox_group(-name=3D>'words',
-values=3D>['eenie','meenie','minie','moe'],
-defaults=3D>['eenie','minie']),
$html -> p,
"What's your favorite color?",
$html -> popup_menu(-name=3D>'color',
-values=3D>['red','green','blue','chartreuse']),
$html -> p,
$html -> submit,
$html -> end_form,
$html -> hr;
$html -> param(-name =3D> 'name', -value =3D> $new_value);
print $html -> end_html;
Post Follow-up to this messageRICHARD FERNANDEZ am Sunday, 24. September 2006 02:15:
>
> Make that line $query->param(-name=>'foo', -value=>"$the_value");
>
> Then somwhere at the start where you get the variables;
>
> my $name = $query->param('name')||'';
>
> if ($name =~/mary/i){ $the_value = "Howdy Mary" }
>
>
>
> How you do it depends on your workflow/logic
>
>
>
> Owen
Hello Richard
> Thanks for your response, but I still don't get it. I type my name into
> the box and click
> Submit, but it doesn't change!
>
> Could you please clarify?
>
> Here's my code:
>
> #!/usr/bin/perl -w
> # This script is the example at the top of the CGI pod --richf;
> use strict;
> use CGI;
>
> my $html = new CGI;
>
> my $new_value;
my $new_value='';
> my $name = $html -> param('name') or '';
This should read:
my $name = $html->param('name') || '';
due to the precedence of the operators: '||' > '=' > 'or'
See
perldoc perlop
('' is never assigned to $new_value with 'or')
And please don't use spaces around '->' :-)
> if ($name =~ /richf/i) {
> $new_value = 'John';
> }
>
> print $html -> header,
> $html -> start_html('A Simple Example'),
> $html -> h1('A Simple Example'),
> $html -> start_form,
>
> "What's your name?",
> $html -> textfield(-name => 'name'),
Here's the source of your problem: the textfield value is missing:
$html->textfield(-name => 'name', -value => $new_value),
> $html -> p,
> "What's the combination?",
> $html -> p,
> $html -> checkbox_group(-name=>'words',
> -values=>['eenie','meenie','minie','moe'],
btw, above line can be written shorter:
-values=>[ qw( eenie meenie minie moe ) ],
> -defaults=>['eenie','minie']),
> $html -> p,
> "What's your favorite color?",
> $html -> popup_menu(-name=>'color',
> -values=>['red','green','blue','chartreuse']),
> $html -> p,
> $html -> submit,
> $html -> end_form,
> $html -> hr;
>
>
>
> $html -> param(-name => 'name', -value => $new_value);
This will only change the value in the request object, but not in the form!
(also part of your problem)
> print $html -> end_html;
Hope this helps!
Dani
Post Follow-up to this message
Did you test it? It is my impression that CGI.pm works this way
normally:
<snip>
if (param) {
# Replace the parameter.
my $temp =3D param('name');
if ($nmap{$temp}) {
# param('name',$nmap{$temp}); # This also works.
param(-name =3D> 'name', -value =3D> $nmap{$temp});
}
print
"Your name is ",em(param('name')),p,
"The keywords are: ",em(join(", ",param('words'))),p,
"Your favorite color is ",em(param('color')),".\n";
}
print end_html;
Thanks for your response, Mumia.=20
This is very similar to the example in the doc, and it does work, but
what I'm (still) trying to do is replace the name that is typed into the
textfield, not print it outside of the form.
Thanks to all who've responded, so far. I'm still playing with it, but
haven't cracked it yet... I hate getting hung up on something simple.
(maybe what I need is sleep at this point :)=20
richf
Post Follow-up to this messageDing! Ding! Ding! I finally got it!!!=20 Not sure why this had me so stumped... In any case, thanks to all that responded. I appreciate all the help. richf
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.