#!/usr/bin/perl -w

# postcard-form.pl, a program that prints out
# an HTML form suitable for creating postcards.

use strict;
use diagnostics;
# available from http://www.perl.com/CPAN/
use CGI;
use Mysql;

# Create an instance of CGI
my $query = new CGI;

# Send an appropriate MIME header
print $query->header("text/html");

# Begin the HTML output
print $query->start_html(-title =>
   "Send a postcard!");

# Connect via Unix sockets to the database on this
# server
my $dbh = Mysql->connect("localhost", "test");

# Build up our SQL command
my $command = "";
$command = "select graphic_id,";
$command .= "graphic_file from graphics";

# Uncomment for debugging
# print "<P>SQL command: <B>$command</B></P>\n";

# Send the query
my $sth = $dbh->query($command);

# Make sure that $sth returned reasonably
die "Error with command \"$command\""
   unless (defined $sth);

# Iterate through the returned rows
my @arr = ();

# Print some HTML
print '<P>Now you can send a Web-based postcard ';
print 'to your friends!  Enter the appropriate ';
print 'information, and e-mail will be sent to ';
print 'your friends indicating where they can pick ';
print 'up their postcard.</P>', "\n";

print '<Form method="POST"';
print ' action="/cgi-bin/send-postcard.pl">';
print ' "\n";

print '<P>Your name: <input type="text" ';
print 'name="sender_name"> </P>';
print '<P>Your e-mail address: <input type="text"'
print ' name="sender_email"> </P>';

print '<P>Recipient's name: <input type="text"'
print 'name="recipient_name"> </P>';

print '<P>Recipient's e-mail address: '
print ' <input type="text" ';
print 'name="recipient_email"> </P>';

print '<P>Name of the graphic to insert: '
print ' <input type="text" ';
print 'name="graphic_name"> </P>';

# Now get the graphics IDs from the database, and
# print them out
while (@arr = $sth->fetchrow)
{
    # Get the ID and filename
    my ($id, $name) = @arr;

    print '"<P><input type="radio"';
    print 'value="$id" ';
    print 'name="graphic_id"'>$name</P>\n";
}

print '<textarea name="postcard_text" ';
print 'wrap="hard" rows="20" cols="60">', "\n";
print '</textarea>';

print '<P><input type="submit" ';
print 'value="Send postcard"></P>', "\n";
print '<P><input type="reset" ';
print 'value="Clear current contents"></P>', "\n";

print "</Form>\n";

# End the HTML
print $query->end_html;

exit;