Hi all,

I'm writing my first CGI script, which is supposed to grab some fields, 
do some error checking, then enter the info into the DB.  The problem 
is, nothing's getting to the DB (I also know my password confirmation 
isn't working, but I'll worry about that later).  Everything else works 
until you get to the confirmation page, but nothing happens there after 
you press submit.

As a starting point, I did a view source on the confirmation page, and 
it's still showing the html source from the page where you originally 
enter your info.

All the PageAlert alert module does is set up some environment variables 
for the DB handle, and I've already run some sample DB calls to make 
sure it was connecting properly.

I already know the code is ugly, so feel free to berate me =)

I'm not sure if the list supports attachements, so I'll re-post if 
necessary.

Thanks.
#!/usr/local/bin/perl -w

#  Simple login page for PageAlert
#
# -jwilliam, 02/22/2002

use CGI qw(:standard);
require "cgi-lib.pl";
require "PageAlert.pm"; 

$dir = '/local/HTML/PageAlert2';

my $okay = 'N';
my $AddToDB = 'N';

my $query = new CGI;

######################
# PREPARE STATEMENTS #
######################

$sth_user_exist=$dbh->prepare("select count(username) from PALERT_USERS
        where username = ?");

$sth_add_user=$dbh->prepare("insert into PALERT_USERS 
        (userid, name, username, password, email, phone, pager) 
        values
        (user_id.nextval, ?, ?, ?, ?, ?, ?)");

########
# MAIN #
########

 MAIN: 
{
#   Read fields from form
    if (param()) {

        $name      = $query -> param("name");
        $username  = $query -> param("username");
        $password  = $query -> param("password");
        $confirm   = $query -> param("confirm");
        $email     = $query -> param("email");
        $phone     = $query -> param("phone");
        $pager     = $query -> param("pager");
        $AddUserOK = $query -> param("AddToDB");

        @fields = ($name, $username, $password, $confirm, $email, $phone, 
                   $pager);

        foreach $field (@fields) {
            chomp ($field);
            $field =~ s|^\s*(\S*)|$1|;
            $field =~ s|(\S*)\s*$|$1|;
        }

#       Check and see if fields look okay
        &Validate;

        if ($okay eq 'N') {
            &PrintAddNewUserText;
            $errormsg = "";
        }
        else { 
            &ConfirmInfo;
        }

        # make sure data comes from confirmation page
        if ($AddUserOK eq "Y") {

            &AddNewUser;
        }

    } else {
        &PrintAddNewUserText;
    }
}


###########################
# SUB PrintAddNewUserText #
###########################

sub PrintAddNewUserText{
    print &PrintHeader;
    print &HtmlTop ("Add New User To PageAlert");
    print $errormsg;
    print hr();
    print p("Please enter the following information: \n");
    print start_form(-action => '/PA2-bin/add_user.cgi',
                     -method => POST), "\n"; 
    print p("Name: ", textfield("name")), "\n";
    print p("Username: ", textfield("username")), "\n";
    print p("Password: ", password_field("password")),"\n";
    print p("Confirm password: ",password_field("confirm")), "\n";
    print p("Email Address: ", textfield("email")), "\n";
    print p("Phone Number: ", textfield("phone")), "\n";
    print p("Cell Phone/Pager Number: ", textfield("pager")), "\n";
    print p(submit("Submit"),reset("Clear")), "\n";
    print hidden(-name=>'AddToDB',
                 -value=>'N'), "\n";
    print end_form, "\n";
    print &HtmlBot, "\n";

}


################
# SUB Validate #
################

sub Validate{
    $errormsg = '<font color="red">';
    if ($username eq  "") {
        $errormsg .= "Please enter a username\n<br>";
        p("Username: ", $username), "\n";
        $okay = 'N';
    }
    if ($password eq "") {
        $errormsg .= "Please enter a password\n<br>";
        $okay = 'N';
    }
    if ($password ne $confirm){
        $errormsg .= "Password confirmation does not match password.\n<br>";
        $password = $query -> delete('password');
        $okay = 'N';
    }
    if ($email !=~ m|[\w-]+@([\w-]+\.)+\w+|) {
        $errormsg .= "Please enter a valid email address.\n<BR>";
        $okay = 'N';
    }
    if ($phone eq "") {
        $errormsg .= "Please enter a phone number\n<br>";
        $okay = 'N';
    }
    if ($pager eq "") {
        $errormsg .= "Please enter a cell phone or pager number\n<br>";
        $okay = 'N';
    }
    else {
        $okay='Y';
    }

    $errormsg .= '</font>';

}




###################
# SUB ConfirmInfo #
###################

sub ConfirmInfo {

    print &PrintHeader;
    print &HtmlTop ("Confirm Account Information"), "\n";
    print start_form(-action => '/PA2-bin/add_user.cgi',
                     -method => POST), "\n"; 
    print p("Please confirm the following information: "), "\n";
    print p("Name: ", $name), "\n";
    print p("Username: ", $username), "\n";
    print p("Email Address: ", $email), "\n";
    print p("Phone Number: ", $phone), "\n";
    print p("Cell Phone/Pager Number: ", $pager), "\n";
    print p(submit("Submit")), "\n";
    print p("<b>Hit back button to make changes to your information.</b>");
    print p(hidden(-name=>'name',
                 -value=>$name)), "\n";
    print p(hidden(-name=>'username',
                 -value=>$username)), "\n";
    print p(hidden(-name=>'password',
                 -value=>$password)), "\n";
    print p(hidden(-name=>'email',
                 -value=>$email)), "\n";
    print p(hidden(-name=>'phone',
                 -value=>$phone)), "\n";
    print p(hidden(-name=>'pager',
                 -value=>$pager)), "\n";
    print p(hidden(-name=>'AddToDB',
                 -value=>'Y')), "\n";
    print end_form, "\n";
    print &HtmlBot;
}


##################
# SUB CheckExist #
##################

sub CheckExist {
    $count = 1;

    $sth_user_exist->execute($username);
    $count=$sth_user_exist->fetchrow;

    if ($count == 0) {
        $user_exists = 'N';
    }
    else {
        $user_exists = 'Y';
    }
}


##################
# SUB AddNewUser #
##################

sub AddNewUser {

    ## Make sure username does not already exist in db
    &CheckExist;

    if ($user_exists eq 'N'){

        ### Insert user info
        $sth_add_user->execute($name, $username, $password, $email, $phone, $pager) || 
print p("Cannot add user for the following reason: $!:");

        &Printheader;
        print p("Username $username has been successfully added to PageAlert");
        print end_form;

        $dbh -> finish;
    }
    else {
        $errormsg = "<H4><font color=maroon>I'm sorry.<br>That username already 
exists. Please try a different username.</font></h4><P>";
        &PrintAddNewUserText;
    }
}



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to