I am tying to figure out this whole form stuff and I have created a form that the user chooses the attributes of an image but I am doing something wrong becuase it doesn't show what I have choosen . Could someone point me in the direction of my mistake.
Here is the code:
#!/usr/bin/perl
=pod
make an image ala our first GD program, with the fill color we choose
=cut
# use this to check the data from a form
use CGI qwstandard);
###print header; # CGI lets us do this
if (!param){
&printform;
}
else{
if (param('test')){
&dump;
}
else{
# &run;
if ( &error_messages ) {
print "Please correct this form and resubmit with the following
items corrected<br>\n";
print $errormsg;
&printform;
}
else{ # no errors
&run;
}
}
}
sub error_messages{
=no checking now
if (param('zip')!~/\A[0-9]{5}\Z/ ){
$errormsg .= "<h2>Error in zip</h2>\n";
$errormsg .= "you need to use 5 numbers<br>\n";
}
if (param('name')!~/[a-zA-Z]/ ){
$errormsg .= "<h2>Error in name</h2>\n";
$errormsg .= "you need to use some letters<br>\n";
}
if (param('phone')!~/(\d{0,3}).*(\d{3}-?\d{4})\Z/){
$errormsg .= "<h2>Error in phone</h2>\n";
$errormsg .= "you need to check the format of the phone
number<br>\n";
}
=cut
}
sub oldrun{
print header;
$color_transparent = param('color_transparent');
print "color transparent is $color_transparent<br>\n";
if ($color_transparent=~/(\d+),(\d+),(\d+)/){
$r = $1;
$g = $2;
$b = $3;
print "r,g,b ($r,$g,$b)<br>\n";;
}
}
sub run{
$color_fill = param('color_fill');
$shape_fill = param('shape_fill');
$size_fill = param('size_fill');
print header('image/png');
use GD;
# create a new image
$im = new GD::Image(100,100);
$im2 = new GD::Image(100,100);
$im3 = new GD::Image(100,100);
$im4 = new GD::Image(100,100);
# allocate some colors
$white = $im->colorAllocate(255,255,255);
$yellow = $im->colorAllocate(255,255,16);
$orange = $im->colorAllocate(255,132,41);
$blue = $im->colorAllocate(8,41,132);
# make the background transparent and interlaced
if ($color_fill=~/(\d+),(\d+),(\d+)/){
$r = $1;
$g = $2;
$b = $3;
$fill = $im->colorAllocate($r,$g,$b);
}
else{
$fill = $red;
}
$im->interlaced('true');
# Put a black frame around the picture
$im3->rectangle(0,0,99,99,$black);
$im4->rectangle(0,0,33,33,$black);
# Draw a blue oval
$im->arc(25,25,40,35,0,360,$blue);
$im2->arc(50,50,95,75,0,360,$blue);
# And fill it with yellow
$im->fill(50,50,$fill) ;
$im2->fill(50,50,$fill) ;
$im3->fill(50,50,$fill) ;
$im4->fill(50,50,$fill) ;
# make sure we are writing to a binary stream
binmode STDOUT;
# Convert the image to GIF and print it on standard output
print $im->png;
}
sub printform{
print header;
$url = url(-relative=>1);
print startform(-method=>'GET',
-action=>$url),"<br>\n";
# create the items needed for our table
$color_fill_prompt = "choose a color to fill circle";
$shape_fill_prompt = "choose a shape to fill the image";
$size_fill_prompt = "choose a size to display the image";
#--------- just get it to work , then get color like we want --------
%labels_color_fill = ('255,255,255'=>'white',
'255,255,16' =>'yellow',
'255,132,41' =>'orange',
'8,41,132' =>'blue');
#%labels_shape_fill = ('im' =>'oval'
#'im2 =>'large');
%labels_size_fill = ('im' =>'small',
'im2' =>'large');
$color_fill = popup_menu(-name=>'color_fill',
-values=>['255,255,255','255,255,16','255,132,41','8,41,132'],
-default=>'255,255,255',
-labels=>\%labels_color_fill);
$shape_fill = popup_menu(-name=>'shape_fill',
-values=>['oval','rectangle'],
-default=>'oval');
$size_fill = popup_menu(-name=>'size_fill',
-values=>['large','small'],
-default=>'large');
$submit_run = submit(-name=>'run',
-value=>'run');
$submit_test = submit(-name=>'test',
-value=>'test');
print <<" ENDTABLE";
<table border=1>
<tr><td>$color_fill_prompt</td> <td>$color_fill</td></tr>
<tr><td>$shape_fill_prompt</td> <td>$shape_fill</td></tr>
<tr><td>$size_fill_prompt</td> <td>$size_fill</td></tr>
<!------------------------------------------------------>
<tr><td>$zip_prompt</td> <td>$zip</td></tr>
<!------------------------------------------------------>
<tr><td>$submit_run</td> <td>$submit_test</td></tr>
</table>
ENDTABLE
print "<a href=\"text.cgi?$url\">the perl code</a><br>\n";
}
sub dump{
print header;
foreach $variable (param){
print "$variable <pre>", param("$variable","</pre> <br>\n";
}
}
__END__
#!/usr/bin/perl
use CGI;
$cgi=new CGI;
Here is the code:
#!/usr/bin/perl
=pod
make an image ala our first GD program, with the fill color we choose
=cut
# use this to check the data from a form
use CGI qwstandard);
###print header; # CGI lets us do this
if (!param){
&printform;
}
else{
if (param('test')){
&dump;
}
else{
# &run;
if ( &error_messages ) {
print "Please correct this form and resubmit with the following
items corrected<br>\n";
print $errormsg;
&printform;
}
else{ # no errors
&run;
}
}
}
sub error_messages{
=no checking now
if (param('zip')!~/\A[0-9]{5}\Z/ ){
$errormsg .= "<h2>Error in zip</h2>\n";
$errormsg .= "you need to use 5 numbers<br>\n";
}
if (param('name')!~/[a-zA-Z]/ ){
$errormsg .= "<h2>Error in name</h2>\n";
$errormsg .= "you need to use some letters<br>\n";
}
if (param('phone')!~/(\d{0,3}).*(\d{3}-?\d{4})\Z/){
$errormsg .= "<h2>Error in phone</h2>\n";
$errormsg .= "you need to check the format of the phone
number<br>\n";
}
=cut
}
sub oldrun{
print header;
$color_transparent = param('color_transparent');
print "color transparent is $color_transparent<br>\n";
if ($color_transparent=~/(\d+),(\d+),(\d+)/){
$r = $1;
$g = $2;
$b = $3;
print "r,g,b ($r,$g,$b)<br>\n";;
}
}
sub run{
$color_fill = param('color_fill');
$shape_fill = param('shape_fill');
$size_fill = param('size_fill');
print header('image/png');
use GD;
# create a new image
$im = new GD::Image(100,100);
$im2 = new GD::Image(100,100);
$im3 = new GD::Image(100,100);
$im4 = new GD::Image(100,100);
# allocate some colors
$white = $im->colorAllocate(255,255,255);
$yellow = $im->colorAllocate(255,255,16);
$orange = $im->colorAllocate(255,132,41);
$blue = $im->colorAllocate(8,41,132);
# make the background transparent and interlaced
if ($color_fill=~/(\d+),(\d+),(\d+)/){
$r = $1;
$g = $2;
$b = $3;
$fill = $im->colorAllocate($r,$g,$b);
}
else{
$fill = $red;
}
$im->interlaced('true');
# Put a black frame around the picture
$im3->rectangle(0,0,99,99,$black);
$im4->rectangle(0,0,33,33,$black);
# Draw a blue oval
$im->arc(25,25,40,35,0,360,$blue);
$im2->arc(50,50,95,75,0,360,$blue);
# And fill it with yellow
$im->fill(50,50,$fill) ;
$im2->fill(50,50,$fill) ;
$im3->fill(50,50,$fill) ;
$im4->fill(50,50,$fill) ;
# make sure we are writing to a binary stream
binmode STDOUT;
# Convert the image to GIF and print it on standard output
print $im->png;
}
sub printform{
print header;
$url = url(-relative=>1);
print startform(-method=>'GET',
-action=>$url),"<br>\n";
# create the items needed for our table
$color_fill_prompt = "choose a color to fill circle";
$shape_fill_prompt = "choose a shape to fill the image";
$size_fill_prompt = "choose a size to display the image";
#--------- just get it to work , then get color like we want --------
%labels_color_fill = ('255,255,255'=>'white',
'255,255,16' =>'yellow',
'255,132,41' =>'orange',
'8,41,132' =>'blue');
#%labels_shape_fill = ('im' =>'oval'
#'im2 =>'large');
%labels_size_fill = ('im' =>'small',
'im2' =>'large');
$color_fill = popup_menu(-name=>'color_fill',
-values=>['255,255,255','255,255,16','255,132,41','8,41,132'],
-default=>'255,255,255',
-labels=>\%labels_color_fill);
$shape_fill = popup_menu(-name=>'shape_fill',
-values=>['oval','rectangle'],
-default=>'oval');
$size_fill = popup_menu(-name=>'size_fill',
-values=>['large','small'],
-default=>'large');
$submit_run = submit(-name=>'run',
-value=>'run');
$submit_test = submit(-name=>'test',
-value=>'test');
print <<" ENDTABLE";
<table border=1>
<tr><td>$color_fill_prompt</td> <td>$color_fill</td></tr>
<tr><td>$shape_fill_prompt</td> <td>$shape_fill</td></tr>
<tr><td>$size_fill_prompt</td> <td>$size_fill</td></tr>
<!------------------------------------------------------>
<tr><td>$zip_prompt</td> <td>$zip</td></tr>
<!------------------------------------------------------>
<tr><td>$submit_run</td> <td>$submit_test</td></tr>
</table>
ENDTABLE
print "<a href=\"text.cgi?$url\">the perl code</a><br>\n";
}
sub dump{
print header;
foreach $variable (param){
print "$variable <pre>", param("$variable","</pre> <br>\n";
}
}
__END__
#!/usr/bin/perl
use CGI;
$cgi=new CGI;