Greetings all,
I do an aweful lot of CGI scripting and have run into this very problem. I would suggest relegating the mail form to a sub within your processing script then you can prepopulate it programatically.
Here is a prepopulation script I wrote early on.
pass it a string containing your form and a reference to a hash containing your data keyed by the values you would find in the corresponding form.
it will return you a string contianing your form prepopulated.
I am positive this can be rewritten more efficiently but I hope this helps.
sub prepopulate_form{
my ($frm_str, $hashref) = @_;
my %values = %$hashref;
my @val_keys = keys(%values);
for(my $i=0;$i<(scalar(@val_keys));$i++){
my $name = $val_keys[$i];
my $frm_elm;
if( $frm_str =~ m/(<(input|textarea)[^>]*name="$name"[^>]*>)/)
+{
$frm_elm = $1;
}elsif( $frm_str =~ m%(<select name="$name".*?/select>)%s ){
$frm_elm = $1;
}
my $new_elm = $frm_elm;
if($frm_elm =~ m/<input/){
my $type = $1 if($frm_elm =~ m/type="([^"]*)"/);
my $value = $1 if($frm_elm =~ m/value="([^"]*)"/);
if($type eq "radio"){
$frm_elm = $1 if($frm_str =~ /(<input[^>]*name="$name"
+[^>]*value="$values{$name}"[^>]*>)/);
$new_elm = $frm_elm;
$new_elm =~ s/>/ checked="true">/;
}elsif($type eq "checkbox"){
if($values{$name} == 1){
$new_elm =~ s/>/ checked>/;
}
}elsif($type =~ m/text/){
if($new_elm =~ /value=/){
$new_elm =~ s/value="[^"]*"/value="$values{$name}"
+/;
}else{
$new_elm =~ s/>/ value="$values{$name}">/;
}
}
}elsif($frm_elm =~ /<textarea/){
$new_elm =~ s/(<textarea[^>]*>)/$1 $values{$name}/;
}elsif($frm_elm =~ /<select/){
$new_elm =~ s/(value="$values{$name}")/$1 selected/s;
}
$frm_elm =~ s/([\-\?\)\(\/])/\\$1/g;
$frm_str =~ s/$frm_elm/$new_elm/s;
}
return $frm_str;
}#end prepopulate_form sub
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.