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!

CGI and Records

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
US
I have created the following program that asks for information about a book and then stores it. However, I would like to enter as many as books as I want ( sometimes 2 or 5) per session and then store them in the file. However, the phone number should be entered only once per session. Is it possible? Please help. Thanks.

#!/usr/bin/perl


use CGI::Carp qw(fatalsToBrowser);

# book.cgi uses bookr.cgi and book1.out

print "Content-type: text/html\n\n";
print &quot;<HTML><TITLE>LocalTime</TITLE>\n&quot;;
print &quot;<BODY BGCOLOR=\&quot;white\&quot;><center>&quot;;
print &quot;Date:....&quot;;

($sec,$min,$hr,$mday,$mon,$year,$wday) = localtime(time);

# now let's make them pretty, suitable for display............
$today = (Sunday,Monday,Tuesday,Wedesday,Thursday,Friday,Saturday)[$wday];
$thismon = (January,Febuary,March,April,May,June,July,
August,September,October,November,December)[$mon];
$year = $year + 1900;

print &quot;$today, $thismon $mday, $year ...Time :... $hr:$min:$sec\n<br>&quot;;

print &quot;</center></BODY></HTML>&quot;;

$d = &quot;\$&quot;;
print &quot;</center></BODY></HTML>&quot;;

use CGI;

# send fatal errors to browser


$query = new CGI;

if (!$query->param()) {
# &print_page_start;
&print_form;
&print_page_end;
}
else {
&set_form_vars;
if (&valid_form eq 'yes') {
&print_success;
&print_page_start;
&print_page_end;
}
else {
&print_page_start;
&print_error_message;
&print_form;
&print_page_end;
}
}

sub print_page_start {
print $query->header;
print &quot;<html>\n&quot;;
print &quot;<head>\n&quot;;
print &quot;<title> Email Form </title>\n&quot;;
print &quot;</head>\n&quot;;
print &quot;<body>\n&quot;;
print &quot;<H1> Email Form </H1>\n&quot;;
}

sub print_page_end {
print &quot;</body>\n&quot;;
print &quot;</html>\n&quot;;
}

sub set_form_vars {
$price = $query->param('phone');
$price = $query->param('price');
$title = $query->param('title');
$author = $query->param('author');
$isbn = $query->param('isbn');
$professor = $query->param('professor');
$edition = $query->param('edition');
$publisher = $query->param('publisher');
$condition = $query->param('condition');

}

sub print_form {
print &quot;<FORM Method=\&quot;post\&quot;>\n&quot;;
print &quot;<P>\nphone:\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;phone\&quot; value=\&quot;$phone\&quot; size=25>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<P>\nTitle of the Book:\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;title\&quot; value=\&quot;$title\&quot; size=25>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<P>\nAuthor of the Book (Last name only):\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;author\&quot; value=\&quot;$author\&quot; size=22>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<P>\nISBN Code: (Not Required but helpful)\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;isbn\&quot; value=\&quot;$isbn\&quot; size=10>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<P>\nEdition:\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;edition\&quot; value=\&quot;$edition\&quot; size=10>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<P>\nName of the publisher:\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;publisher\&quot; value=\&quot;$publisher\&quot; size=25>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<P>\nCondition of the Book (Check one):;\n&quot;;
print &quot;Excellent:\n&quot;;
print &quot;<input type=\&quot;radio\&quot; name=\&quot;condition\&quot; value=\&quot;excellent\&quot;>\n&quot;;
print &quot;Good:\n&quot;;
print &quot;<input type=\&quot;radio\&quot; name=\&quot;condition\&quot; value=\&quot;good\&quot;>\n&quot;;
print &quot;Fair\n&quot;;
print &quot;<input type=\&quot;radio\&quot; name=\&quot;condition\&quot; value=\&quot;fair\&quot;>\n&quot;;
print &quot;Bad:\n&quot;;
print &quot;<input type=\&quot;radio\&quot; name=\&quot;condition\&quot; value=\&quot;bad\&quot;>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<P>\nName of the course the book is used for (use code ,e.g. EHS 397):\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;course\&quot; value=\&quot;$course\&quot; size=15>\n&quot;;
print &quot;</P>\n&quot;;
print &quot;<INPUT TYPE=\&quot;submit\&quot; VALUE=\&quot;Submit\&quot;>\n&quot;;
print &quot;<INPUT TYPE=\&quot;Reset\&quot; VALUE=\&quot;Reset\&quot;></P>\n&quot;;
print &quot;</FORM>\n&quot;;
}

sub valid_form {
$success = &quot;yes&quot;;
if (!$author){
$author_error = &quot;<P> You need to enter author name.</P>\n&quot;;
$success =&quot;no&quot;;
}
if (!$title) {
$title_error = &quot;<P> You need to enter the title of the book.</P>\n&quot;;
$success =&quot;no&quot;;
}
if (!$course) {
$course_error = &quot;<P> You need to enter the course name.</P>\n&quot;;
$success =&quot;no&quot;;
}
if (!$professor) {
$professor_error = &quot;<P> You need to enter the name of the professor using this book.</P>\n&quot;;
$success =&quot;no&quot;;
}
if ($edition_error) {
$edition_error = &quot;<P> You need to specify the edition of the book.</P>\n&quot;;
$success =&quot;no&quot;;
}
if ($condition_error) {
$condition_error = &quot;<P> You need to specify the condition of the book.</P>\n&quot;;
$success =&quot;no&quot;;
}

return $success;
}

sub print_error_message {

if ($author_error) {
print $author_error, &quot;\n&quot;;
}
if ($title_error) {
print $title_error, &quot;\n&quot;;
}
if ($course_error) {
print $course_error, &quot;\n&quot;;
}
if ($professor_error) {
print $professor_error, &quot;\n&quot;;
}
if ($edition_error) {
print $edition_error, &quot;\n&quot;;
}
if ($condition_error) {
print $price_error, &quot;\n&quot;;
}
if ($price_error) {
print $price_error, &quot;\n&quot;;
}

}

sub print_success {

$d =&quot;\$&quot;;
$de =&quot;\$1&quot;;
$dd =&quot;\$30&quot;;

open(OUTF,&quot;>>book1.out&quot;) or dienice(&quot;Couldn't open survey.out for
writing: $!&quot;);

# This locks the file so no other CGI can write to it at the
# same time...
flock(OUTF,2);
# Reset the file pointer to the end of the file, in case
# someone wrote to it while we waited for the lock...
seek(OUTF,0,2);

print OUTF &quot;$phone|&quot;;
print OUTF &quot;$title|&quot;;
print OUTF &quot;$author|&quot;;
print OUTF &quot;$isbn|&quot;;
print OUTF &quot;$edition|&quot;;
print OUTF &quot;$publisher|&quot;;
print OUTF &quot;$condition|&quot;;
print OUTF &quot;$course|&quot;;
print OUTF &quot;$professor|&quot;;
print OUTF &quot;$price|&quot;;
close(OUTF);

}

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


</body></html>
EndHTML
}
 
Since your using global variables (bad thing but your doing it so lets work with it)

Just do

if(!exists($phone)){
print &quot;<P>\nphone:\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;phone\&quot; value=\&quot;$phone\&quot; size=25>\n&quot;;
}

That will check for the existence of the $phone variable. If it has not been defined it will display the field. If it has been defined it will not.

However, you are pre-popualting these fields so from a workflow perspective what you have accomplishes the same thing, it only asks for it 'once' really.

 
Since I am new to CGI programming and I copy these programs from CGI books, so I do not know why you feel that the method I used is not appropriate. But, I am not sure what this statement does to continue asking for the information about different books. I do not have loop in my program so I am so sure that it will function as a loop. I appreciate your time and help. If you could explain to me these steps. Thanks again. I will try your step any way and will let you know whether it worked or not.
 
For your knowledge : Using global variables is not really acceptable practice, your CGI book is either out-dated or meant for the extreme beginner.

That said, I still do not really know what you are trying to do.

This may be better for your situation upon reading over what I suggested.

if(!exists($phone)){
print &quot;<P>\nphone:\n&quot;;
print &quot;<input type=\&quot;text\&quot; name=\&quot;phone\&quot; value=\&quot;$phone\&quot; size=25>\n&quot;;
} else {
print &quot;<input type=\&quot;hidden\&quot; name=\&quot;phone\&quot; value=\&quot;$phone\&quot; size=25>\n&quot;;
}

That will hide the field if they already input it AND keep it in your CGI submission so your loops function properly.
 
Thanks for your suggestions. I feel that I need to clarify the situation further. What I want to do is to allow a user to enter the information about as many books as possible up to price, and when done then enter phone number and email. That means that user should loop the title of the book and the rest when done should enter the rest of the information. I hope this clarifies the situation.
 
I suggest re-writing the CGI to be based around this model.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top