Forum Moderators: coopster & phranque

Message Too Old, No Replies

Dynamic Form Validation

         

scoobie

12:10 am on Dec 16, 2005 (gmt 0)

10+ Year Member



Hi,
i am trying to create a dynamic validation form. basically what i want the form to be able to do is dispplay a message when a particular field is not filled in without it actually generating a new page with one line. here is the code i tried and am failing to get it to work:

#!/usr/local/bin/perl
#print "Content-type: text/html\n\n";
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use DBI;
use strict;

my $query = new CGI;

my $HOST = "localhost";
my $DATABASE = "db";
my $USERNAME = "us";
my $PASSWORD = "pw";
my $dsn = "DBI:mysql:$DATABASE:$HOST";
my $DB = DBI->connect($dsn, $USERNAME, $PASSWORD, {});
print $query->header ( );

# Process form if submitted; otherwise display it
if (param())
{
my $error_message = "";
$error_message .= "Please enter your First Name<br>" if (!$firstname );

$error_message .= "Please enter your Last Name<br>" if (!$lastname );

$error_message .= "Please enter your E-mail Address<br>" if (!$email );

if ( $error_message )
{
display_form ( $error_message, $firstname, $lastname, $email);
}
else
{
# Form OK - return success
#return 1;
my $insert_qry = "insert into table(fname, lname, email) values(?,?,?)";
my $sth = $DB->prepare($insert_qry);
$sth->execute($firstname, $lastname, $email);
my $url = "http://www.google.com";
print "Location: $url\n\n";
}
}
sub display_form
{
my $error_message = shift;
my $firstname = shift;
my $lastname = shift;
my $email = shift;

# Display the form
print <<ENDHTML;

<form method="post" action = "reg.pl">
First Name:<input type="text" name=FName size="30">
SurName:<input type="text" name=LName size="30">
E-mail:<input type="text" name=Email size="30">
<br>
<!-- runs the insert query when clicked -->
<button type = "submit" name = insert>
Submit Details
</button>
</form>
ENDHTML
}

when i run this code it doesn't update the database or display error msg. i can't think of any thing else to try

Can anybody help?

thanks in advanced,

sc

bennymack

12:56 pm on Dec 16, 2005 (gmt 0)

10+ Year Member



I see several problems with that script. For one, it shouldn't even compile under strict.

You need to declare $firstname, $lastname, and $email as lexical variables with 'my'.

You need to assign them the values posted to the form:
$firstname=$query->param('FName'), etc...

I don't know if <button type=submit>Submit Details</button> will actually work..

Try <input type='submit' value='Submit Details' />

You pull in $error_message, $firstname, $lastname, $email to your display_form sub but never use them.

Good luck

GranoSalis

10:28 am on Dec 19, 2005 (gmt 0)

10+ Year Member



Maybe you'd be better off with JavaScript - open an Alert window for the user saying 'please complete all fields marked compulsory' or something like that.
HTMLSource's article:
[yourhtmlsource.com...]
Good luck!

scoobie

11:18 am on Dec 19, 2005 (gmt 0)

10+ Year Member



hi,

i got that to work. thanks everyone for all the help,

scoobie