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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI and QUERY Statement

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
0
0
US
I have created the following program. I want the program to ask the user to answer the first set of options. If they click the whole list, it goes to print_start_select, and if they click the choice option, it goes to the print_page_start. However, it shows all options and goes to cgi-bin/sub. Is it possible to do such a thing, and how? Please help.

#!/usr/bin/perl -w

print "Content-type:text/html\n\n";

use CGI;
$query = new CGI;

#testsub.cgi

print &quot;<h2> FOR SALE</h2>\n&quot;;

# First we initialize some counters and hashes for storing the
# summarized data.
$countt =0;
$countt++;
$d =&quot;\$&quot;;

print &quot;<a href=\&quot;&print_page_start\&quot;> Do you want to choose by price? <a href=\&quot;&print_start_select\&quot;> Do you want to print the whole list of available product?\n<br>&quot;;

if ($query->param()) {
$search = $query->param('name');
print &quot;<br><br>\n&quot;;

&print_start_select1;


# Wrap this code is an eval block in order to trap error

sub print_start_select {
print &quot;all\n&quot;;
}

sub print_start_select1{
print &quot;select\n&quot;;
}

}

else {
print &quot;<FORM>\n&quot;;
print &quot;<INPUT TYPE=\&quot;text\&quot; NAME=\&quot;search\&quot;>Type the model, price, or year of the product?<BR>\n&quot;;
print &quot;<input type=\&quot;submit\&quot;> <input type=\&quot;reset\&quot;>\n&quot;;
print &quot;<FORM>\n&quot;;
}



sub print_page_start {

print $query->header;
print &quot;<html>\n<head>\n<TITLE>Search for Records</TITLE>\n&quot;;
print &quot;</HEAD>\n<BODY>\n&quot;;
print &quot;<h5><font color=\&quot;blue\&quot;>HERE IS ONE \n&quot;;
print &quot;<b><font color=\&quot;black\&quot;> YOU CAN LIST
E-MAIL.</h5>\n&quot;;
print &quot;<H4><font color=\&quot;red\&quot;> click Submit Query</font>
</H4>\n&quot;;
}


print &quot;</body>\n</html>\n&quot;;

 
There are a number of problems in this script.

Why are you defining subroutines within an 'if' block?

Your logic seems a bit off. You have no clear line of control in this script.

Here is my version, you fill in the blanks, and it ignores all those useless count variables. This is a good shell for you to work with.

----


#!/usr/bin/perl -w

use CGI;
$query = new CGI;

print $query->header ;

if( $query->param('whole_list') ) {
# If they clicked the 'whole_list link show the whole list
whole_list();

} elsif( $query->param('price') ) {
# If the clicked hte price list show the price
price_list() ;

} else {
# Otherwise show their options
default_options() ;
}


### Subroutines that the control block uses


sub whole_list {
print &quot;Doing the whole list option here
}

sub price_list {
print &quot;Doing the price list option here&quot; ;
}

sub default_options {
print &quot;<a href=\&quot;?price=1\&quot;> Do you want to choose by price?</A> <a href=\&quot;?whole_list=1\&quot;>Do you want to print the whole list of available product?</A>\n<br>&quot;;
}
 
Thank you very much. It worked, I would have never thought of the statements you created. I appreciate your time. Thanks.
 
This is a nice 'generalized' architecture for any CGI app. CGI apps are 'request/response' due to the nature of the HTTP protocol. This structure lets you add in new actions etc very easily and support them with subsequent functions at the bottom.

Enjoy! Glad it helped.
 
Thanks for helping me with the following program. However, I would like the program to go to another part of the program to allow user to enter criteria for the search. However, this prgram prints heading &quot;FOR SALE&quot; and does not ask for any input. Please help.


#!/usr/bin/perl -w

use CGI;
$query = new CGI;
print $query->header;

#testsub.cgi

print &quot;<h2> FOR SALE</h2>\n&quot;;

# First we initialize some counters and hashes for storing the
# summarized data.
$countt =0;
$countt++;
$d =&quot;\$&quot;;

print $query->header;
if( $query->param('whole_list') ) {
# If they clicked the 'whole_list link show the whole list
whole_list();
}

# if this is true, I want it to allow user to specify price, item etc. in the subroutine price_list.

elsif( $query->param('price') ) {
# If the clicked hte price list show the price
price_list() ;
}

else {
# Otherwise show their options
default_options() ;
}

sub whole_list {

eval {
open(GUEST, &quot;<truc.out&quot;) or dienice(&quot;Couldn't open auto.out for reading: $! \n&quot;);
while (<GUEST>) {
chomp;
($name,$add,$city,$state,$zip,$make,$photo,$model,$color,$mile,
$eyear,$price,$email,$tele,$time,$comment1,$month,$day,$year)=split/\|/;


# this is the same as $count = $count + 1;

$countt++;

if ($photo eq 'Yes') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th width=\&quot;130\&quot;>Picture <font size=\&quot;1\&quot; >(to enlarge: right click the picture, then view image)</font> </tr>\n&quot;;
}

if ($photo eq 'No') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th>Year</th><th>Make</th><th>Model</th><th>Color</th><th>Miles</th>\n&quot;;

}
}
}
}

#New sub;

sub price_list {

eval {
open(GUEST, &quot;<truc.out&quot;) or dienice(&quot;Couldn't open auto.out for reading: $! \n&quot;);
while (<GUEST>) {
chomp;
($name,$add,$city,$state,$zip,$make,$photo,$model,$color,$mile,
$eyear,$price,$email,$tele,$time,$comment1,$month,$day,$year)=split/\|/;

$countt++;

# this is the same as $count = $count + 1;

if (/$search_name/) {

if ($photo eq 'Yes') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th width=\&quot;130\&quot;>Picture <font size=\&quot;1\&quot; color=\&quot;green\&quot;>(to enlarge: right click the picture, then view image)</font></th>\n&quot;;
}

if ($photo eq 'No') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th>Year</th><th>Make</th><th>Model</th><th>Color</th><th>Miles</th>\n&quot;;
}
}
}
}
}

sub price_list {

print $query->header;
print &quot;<html>\n<head>\n<TITLE>Search for Records</TITLE>\n&quot;;
print &quot;</HEAD>\n<BODY>\n&quot;;

print &quot;<img src=\&quot;/images/9897736497.jpg\&quot; width=\&quot;250\&quot; height=\&quot;250\&quot; align=\&quot;left\&quot; vspace=\&quot;5\&quot; hspace=\&quot;5\&quot;>\n&quot;;
print &quot;<h5><font color=\&quot;blue\&quot;>HERE IS ONE \n&quot;;
print &quot;<FORM>\n&quot;;
print &quot;<INPUT TYPE=\&quot;text\&quot; NAME=\&quot;search_name\&quot;><BR>\n&quot;;
print &quot;<INPUT TYPE=\&quot;submit\&quot;>\n&quot;;
print &quot;<FORM>\n&quot;;

}

sub dienice {
my($msg) = @_;
print &quot;<h2>Error</h2>\n&quot;;
print $msg;
exit;
}
 
Great so add another elsif

elsif( $query->param(search_name) ) {
# If they entered search criteria do that
search_name() ;
}

sub search_name {
# do the search
}


See, you just have to add elsif and then a subroutine to handle the elsif....

 
I have added your suggestion. However, it does not work. I am sure it is my mistake. But I do not how the if and sub work in two locations. Please help. I am asking too much help. But if you can, I would appreciate it. Thanks.


#!/usr/bin/perl -w

use CGI;
$query = new CGI;
print $query->header;

#testsub.cgi

print &quot;<h2> FOR SALE</h2>\n&quot;;

# First we initialize some counters and hashes for storing the
# summarized data.
$countt =0;
$countt++;
$d =&quot;\$&quot;;

print $query->header;
if( $query->param('whole_list') ) {
# If they clicked the 'whole_list link show the whole list
whole_list();
}

elsif( $query->param('price') ) {
# If the clicked hte price list show the price
price_list() ;
}

elsif( $query->param(search_name) ) {
# If they entered search criteria do that
search_name() ;
}

else {
# Otherwise show their options
default_options() ;
}

sub whole_list {

eval {
open(GUEST, &quot;<truc.out&quot;) or dienice(&quot;Couldn't open auto.out for reading: $! \n&quot;);
while (<GUEST>) {
chomp;
($name,$add,$city,$state,$zip,$make,$photo,$model,$color,$mile,
$eyear,$price,$email,$tele,$time,$comment1,$month,$day,$year)=split/\|/;


# this is the same as $count = $count + 1;

$countt++;

if ($photo eq 'Yes') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th width=\&quot;130\&quot;>Picture <font size=\&quot;1\&quot; >(to enlarge: right click the picture, then view image)</font> </tr>\n&quot;;
}

if ($photo eq 'No') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th>Year</th><th>Make</th><th>Model</th><th>Color</th><th>Miles</th>\n&quot;;

}
}
}
}

#New sub;

sub search_name {

eval {
open(GUEST, &quot;<truc.out&quot;) or dienice(&quot;Couldn't open auto.out for reading: $! \n&quot;);
while (<GUEST>) {
chomp;
($name,$add,$city,$state,$zip,$make,$photo,$model,$color,$mile,
$eyear,$price,$email,$tele,$time,$comment1,$month,$day,$year)=split/\|/;

$countt++;

# this is the same as $count = $count + 1;

if (/$search_name/) {

if ($photo eq 'Yes') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th width=\&quot;130\&quot;>Picture <font size=\&quot;1\&quot; color=\&quot;green\&quot;>(to enlarge: right click the picture, then view image)</font></th>\n&quot;;
}

if ($photo eq 'No') {
print &quot;<table width=\&quot;100%\&quot;>\n&quot;;
print &quot;<tr bgcolor=\&quot;red\&quot;><th>Year</th><th>Make</th><th>Model</th><th>Color</th><th>Miles</th>\n&quot;;
}
}
}
}
}

sub price_list {

print $query->header;
print &quot;<html>\n<head>\n<TITLE>Search for Records</TITLE>\n&quot;;
print &quot;</HEAD>\n<BODY>\n&quot;;

print &quot;<img src=\&quot;/images/9897736497.jpg\&quot; width=\&quot;250\&quot; height=\&quot;250\&quot; align=\&quot;left\&quot; vspace=\&quot;5\&quot; hspace=\&quot;5\&quot;>\n&quot;;
print &quot;<h5><font color=\&quot;blue\&quot;>HERE IS ONE \n&quot;;
print &quot;<FORM>\n&quot;;
print &quot;<INPUT TYPE=\&quot;text\&quot; NAME=\&quot;search_name\&quot;><BR>\n&quot;;
print &quot;<INPUT TYPE=\&quot;submit\&quot;>\n&quot;;
print &quot;<FORM>\n&quot;;

}

sub dienice {
my($msg) = @_;
print &quot;<h2>Error</h2>\n&quot;;
print $msg;
exit;
 
You have no 'default_options()' subroutine defined.

This means that the 'else' statement effectively does nothing, giving you your lack of output.
 
and why dousnt any one use CGI like its ment
to be.
Like $q->header(),start_html etc...;
it would make scripts much more readble up2date with html genartion..and life much easyer.
and read some books too about sub being on the bottum of the script
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top