Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Just learning CGI

Status
Not open for further replies.

Gwen21

Programmer
Apr 5, 2002
16
US
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 qw:)standard);
###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&quot;;
print $errormsg;
&printform;
}
else{ # no errors
&run;
}
}
}
sub error_messages{
=no checking now
if (param('zip')!~/\A[0-9]{5}\Z/ ){
$errormsg .= &quot;<h2>Error in zip</h2>\n&quot;;
$errormsg .= &quot;you need to use 5 numbers<br>\n&quot;;
}
if (param('name')!~/[a-zA-Z]/ ){
$errormsg .= &quot;<h2>Error in name</h2>\n&quot;;
$errormsg .= &quot;you need to use some letters<br>\n&quot;;
}
if (param('phone')!~/(\d{0,3}).*(\d{3}-?\d{4})\Z/){
$errormsg .= &quot;<h2>Error in phone</h2>\n&quot;;
$errormsg .= &quot;you need to check the format of the phone
number<br>\n&quot;;
}
=cut
}
sub oldrun{
print header;
$color_transparent = param('color_transparent');
print &quot;color transparent is $color_transparent<br>\n&quot;;
if ($color_transparent=~/(\d+),(\d+),(\d+)/){
$r = $1;
$g = $2;
$b = $3;
print &quot;r,g,b ($r,$g,$b)<br>\n&quot;;;
}

}
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),&quot;<br>\n&quot;;
# create the items needed for our table

$color_fill_prompt = &quot;choose a color to fill circle&quot;;
$shape_fill_prompt = &quot;choose a shape to fill the image&quot;;
$size_fill_prompt = &quot;choose a size to display the image&quot;;
#--------- 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 <<&quot; ENDTABLE&quot;;
<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 &quot;<a href=\&quot;text.cgi?$url\&quot;>the perl code</a><br>\n&quot;;
}
sub dump{
print header;
foreach $variable (param){
print &quot;$variable <pre>&quot;, param(&quot;$variable&quot;),&quot;</pre> <br>\n&quot;;
}
}
__END__
#!/usr/bin/perl
use CGI;
$cgi=new CGI;


 
There is a faq in this forum that works through some simple examples (see the faq tab at the top of the front page of the forum). I don't have time at the moment to proof your code..... maybe tomorrow. Hopefully, one of the faqs will be of some assistance in the mean time. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks for the the tip I'll check out the FAQ's
Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top