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 "<HTML><TITLE>LocalTime</TITLE>\n";
print "<BODY BGCOLOR=\"white\"><center>";
print "Date:....";
($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 "$today, $thismon $mday, $year ...Time :... $hr:$min:$sec\n<br>";
print "</center></BODY></HTML>";
$d = "\$";
print "</center></BODY></HTML>";
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 "<html>\n";
print "<head>\n";
print "<title> Email Form </title>\n";
print "</head>\n";
print "<body>\n";
print "<H1> Email Form </H1>\n";
}
sub print_page_end {
print "</body>\n";
print "</html>\n";
}
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 "<FORM Method=\"post\">\n";
print "<P>\nphone:\n";
print "<input type=\"text\" name=\"phone\" value=\"$phone\" size=25>\n";
print "</P>\n";
print "<P>\nTitle of the Book:\n";
print "<input type=\"text\" name=\"title\" value=\"$title\" size=25>\n";
print "</P>\n";
print "<P>\nAuthor of the Book (Last name only):\n";
print "<input type=\"text\" name=\"author\" value=\"$author\" size=22>\n";
print "</P>\n";
print "<P>\nISBN Code: (Not Required but helpful)\n";
print "<input type=\"text\" name=\"isbn\" value=\"$isbn\" size=10>\n";
print "</P>\n";
print "<P>\nEdition:\n";
print "<input type=\"text\" name=\"edition\" value=\"$edition\" size=10>\n";
print "</P>\n";
print "<P>\nName of the publisher:\n";
print "<input type=\"text\" name=\"publisher\" value=\"$publisher\" size=25>\n";
print "</P>\n";
print "<P>\nCondition of the Book (Check one):;\n";
print "Excellent:\n";
print "<input type=\"radio\" name=\"condition\" value=\"excellent\">\n";
print "Good:\n";
print "<input type=\"radio\" name=\"condition\" value=\"good\">\n";
print "Fair\n";
print "<input type=\"radio\" name=\"condition\" value=\"fair\">\n";
print "Bad:\n";
print "<input type=\"radio\" name=\"condition\" value=\"bad\">\n";
print "</P>\n";
print "<P>\nName of the course the book is used for (use code ,e.g. EHS 397):\n";
print "<input type=\"text\" name=\"course\" value=\"$course\" size=15>\n";
print "</P>\n";
print "<INPUT TYPE=\"submit\" VALUE=\"Submit\">\n";
print "<INPUT TYPE=\"Reset\" VALUE=\"Reset\"></P>\n";
print "</FORM>\n";
}
sub valid_form {
$success = "yes";
if (!$author){
$author_error = "<P> You need to enter author name.</P>\n";
$success ="no";
}
if (!$title) {
$title_error = "<P> You need to enter the title of the book.</P>\n";
$success ="no";
}
if (!$course) {
$course_error = "<P> You need to enter the course name.</P>\n";
$success ="no";
}
if (!$professor) {
$professor_error = "<P> You need to enter the name of the professor using this book.</P>\n";
$success ="no";
}
if ($edition_error) {
$edition_error = "<P> You need to specify the edition of the book.</P>\n";
$success ="no";
}
if ($condition_error) {
$condition_error = "<P> You need to specify the condition of the book.</P>\n";
$success ="no";
}
return $success;
}
sub print_error_message {
if ($author_error) {
print $author_error, "\n";
}
if ($title_error) {
print $title_error, "\n";
}
if ($course_error) {
print $course_error, "\n";
}
if ($professor_error) {
print $professor_error, "\n";
}
if ($edition_error) {
print $edition_error, "\n";
}
if ($condition_error) {
print $price_error, "\n";
}
if ($price_error) {
print $price_error, "\n";
}
}
sub print_success {
$d ="\$";
$de ="\$1";
$dd ="\$30";
open(OUTF,">>book1.out" or dienice("Couldn't open survey.out for
writing: $!"
# 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 "$phone|";
print OUTF "$title|";
print OUTF "$author|";
print OUTF "$isbn|";
print OUTF "$edition|";
print OUTF "$publisher|";
print OUTF "$condition|";
print OUTF "$course|";
print OUTF "$professor|";
print OUTF "$price|";
close(OUTF);
}
sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
</body></html>
EndHTML
}
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
# book.cgi uses bookr.cgi and book1.out
print "Content-type: text/html\n\n";
print "<HTML><TITLE>LocalTime</TITLE>\n";
print "<BODY BGCOLOR=\"white\"><center>";
print "Date:....";
($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 "$today, $thismon $mday, $year ...Time :... $hr:$min:$sec\n<br>";
print "</center></BODY></HTML>";
$d = "\$";
print "</center></BODY></HTML>";
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 "<html>\n";
print "<head>\n";
print "<title> Email Form </title>\n";
print "</head>\n";
print "<body>\n";
print "<H1> Email Form </H1>\n";
}
sub print_page_end {
print "</body>\n";
print "</html>\n";
}
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 "<FORM Method=\"post\">\n";
print "<P>\nphone:\n";
print "<input type=\"text\" name=\"phone\" value=\"$phone\" size=25>\n";
print "</P>\n";
print "<P>\nTitle of the Book:\n";
print "<input type=\"text\" name=\"title\" value=\"$title\" size=25>\n";
print "</P>\n";
print "<P>\nAuthor of the Book (Last name only):\n";
print "<input type=\"text\" name=\"author\" value=\"$author\" size=22>\n";
print "</P>\n";
print "<P>\nISBN Code: (Not Required but helpful)\n";
print "<input type=\"text\" name=\"isbn\" value=\"$isbn\" size=10>\n";
print "</P>\n";
print "<P>\nEdition:\n";
print "<input type=\"text\" name=\"edition\" value=\"$edition\" size=10>\n";
print "</P>\n";
print "<P>\nName of the publisher:\n";
print "<input type=\"text\" name=\"publisher\" value=\"$publisher\" size=25>\n";
print "</P>\n";
print "<P>\nCondition of the Book (Check one):;\n";
print "Excellent:\n";
print "<input type=\"radio\" name=\"condition\" value=\"excellent\">\n";
print "Good:\n";
print "<input type=\"radio\" name=\"condition\" value=\"good\">\n";
print "Fair\n";
print "<input type=\"radio\" name=\"condition\" value=\"fair\">\n";
print "Bad:\n";
print "<input type=\"radio\" name=\"condition\" value=\"bad\">\n";
print "</P>\n";
print "<P>\nName of the course the book is used for (use code ,e.g. EHS 397):\n";
print "<input type=\"text\" name=\"course\" value=\"$course\" size=15>\n";
print "</P>\n";
print "<INPUT TYPE=\"submit\" VALUE=\"Submit\">\n";
print "<INPUT TYPE=\"Reset\" VALUE=\"Reset\"></P>\n";
print "</FORM>\n";
}
sub valid_form {
$success = "yes";
if (!$author){
$author_error = "<P> You need to enter author name.</P>\n";
$success ="no";
}
if (!$title) {
$title_error = "<P> You need to enter the title of the book.</P>\n";
$success ="no";
}
if (!$course) {
$course_error = "<P> You need to enter the course name.</P>\n";
$success ="no";
}
if (!$professor) {
$professor_error = "<P> You need to enter the name of the professor using this book.</P>\n";
$success ="no";
}
if ($edition_error) {
$edition_error = "<P> You need to specify the edition of the book.</P>\n";
$success ="no";
}
if ($condition_error) {
$condition_error = "<P> You need to specify the condition of the book.</P>\n";
$success ="no";
}
return $success;
}
sub print_error_message {
if ($author_error) {
print $author_error, "\n";
}
if ($title_error) {
print $title_error, "\n";
}
if ($course_error) {
print $course_error, "\n";
}
if ($professor_error) {
print $professor_error, "\n";
}
if ($edition_error) {
print $edition_error, "\n";
}
if ($condition_error) {
print $price_error, "\n";
}
if ($price_error) {
print $price_error, "\n";
}
}
sub print_success {
$d ="\$";
$de ="\$1";
$dd ="\$30";
open(OUTF,">>book1.out" or dienice("Couldn't open survey.out for
writing: $!"
# 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 "$phone|";
print OUTF "$title|";
print OUTF "$author|";
print OUTF "$isbn|";
print OUTF "$edition|";
print OUTF "$publisher|";
print OUTF "$condition|";
print OUTF "$course|";
print OUTF "$professor|";
print OUTF "$price|";
close(OUTF);
}
sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
</body></html>
EndHTML
}