Dear forum:
I'm trying to do several basic things with an HTML form and PHP. So far,
I've gotten the form to write to a MySQL database, send email and echo the
fields after the form is submitted.
Next I'd just like to confirm that the fields are not blank and send a
custom error message. But I think I'm confused about where to place the
code.
I've purchased four books on PHP and I've been working for four days and I'm
so stuck. Am I missing some online source for information? I've been to
PHP.net and have not found answers.
The two files below are the HTML and PHP files that are working. The third
file is one of my attempts to check for a blank field and write a custom
error message.
*HTML file***
HTML
HEAD
TITLEAdd a Record/TITLE
/HEAD
BODY
H1Adding a Record to tbltest/H1
FORM METHOD=post ACTION=do_addrecord3.php
TABLE CELLSPACING=3 CELLPADDING=3
TR
TD VALIGN=TOP
PFirst NameBR
INPUT TYPE=text NAME=firstname SIZE=30 MAXLENGTH=225/p
PLast NameBR
INPUT TYPE=text NAME=lastname SIZE=30 MAXLENGTH=225/P
PEmailBR
INPUT TYPE=text NAME=email SIZE=30 MAXLENGTH=225/P
/TD
TR
TD VALIGN=TOP COLSPAN=2 ALIGN=CENTER
/P
PINPUT TYPE=SUBMIT NAME=submit VALUE=Add Record/P
/TD
/TR
/TABLE
/FORM
/BODY
/HTML
*** Working PHP form***
?
/* Check to see if info is there */
if ((!$lastname) || (!$firstname) || (!$email)) {
header(Location: http://localhost/show_addrecord3.html;);
exit;
}
/* Login to database */
$db_name = drip_dripirrigationdb;
$table_name = tbltest;
$connection = @mysql_connect(localhost, drip, xx)
or die(Couldn't connect.);
$db = @mysql_select_db($db_name, $connection)
or die(Couldn't select database.);
/* Insert data */
$sql = INSERT INTO $table_name
(firstname, lastname, email)
VALUES
(\$firstname\, \$lastname\, \$email\)
;
$result = @mysql_query($sql,$connection)
or die(Couldn't execute query.);
?
?
/* Define variables */
$msg = E-MAIL SENT FROM WWW SITE\n;
$msg .= First Name:$firstname\n;
$msg .= Sender's E-Mail: $email\n;
$msg .= Last Name: $lastname\n\n;
$to = [EMAIL PROTECTED];
$subject = Web Site Feedback;
$mailheaders = From: My Web Site \n;
$mailheaders .= Reply-To: $email\n\n;
/* send the mail */
mail($to, $subject, $msg, $mailheaders);
?
HTML
HEAD
TITLEAdd a Record/TITLE
/HEAD
BODY
H1Adding a Record to ? echo $table_name; ?/H1
TABLE CELLSPACING=3 CELLPADDING=3
TR
TD VALIGN=TOP
PSTRONGLast Name:/STRONGBR
!-- Reprint the info on next page --
? echo $lastname; ?/p
PSTRONGFirst Name:/STRONGBR
? echo $firstname; ?/P
PSTRONGEmail:/STRONGBR
? echo $email; ?/P
/TD
!-- repeat --
Pa href=show_addrecord3.htmlClick here to select your username and
password./a/p
/TD
/TR
/TABLE
/BODY
/HTML
Modified PHP form**
?
/* Check to see if info is there */
if ($firstname == ) {
$name_err = font color=redPlease enter your name!/fontbr;
$send = no;
}
if ($lastname == ) {
$lastname_err = font color=redPlease enter a message!/fontbr;
$send = no;
}
if ($email == ) {
$email_err = font color=redPlease enter your e-mail
address!/fontbr;
$send = no;
}
if ($send != no) {
/* Login to database */
$db_name = drip_dripirrigationdb;
$table_name = tbltest;
$connection = @mysql_connect(localhost, drip, )
or die(Couldn't connect.);
$db = @mysql_select_db($db_name, $connection)
or die(Couldn't select database.);
/* Insert data */
$sql = INSERT INTO $table_name
(firstname, lastname, email)
VALUES
(\$firstname\, \$lastname\, \$email\)
;
$result = @mysql_query($sql,$connection)
or die(Couldn't execute query.);
?
?
/* Define variables */
$msg = E-MAIL SENT FROM WWW SITE\n;
$msg .= First Name:$firstname\n;
$msg .= Sender's E-Mail: $email\n;
$msg .= Last Name: $lastname\n\n;
$to = [EMAIL PROTECTED];
$subject = Web Site Feedback;
$mailheaders = From: My Web Site \n;
$mailheaders .= Reply-To: $email\n\n;
/* send the mail */
mail($to, $subject, $msg, $mailheaders);
?
HTML
HEAD
TITLEAdd a Record/TITLE
/HEAD
BODY
H1Adding a Record to ? echo $table_name; ?/H1
TABLE CELLSPACING=3 CELLPADDING=3
TR
TD VALIGN=TOP
PSTRONGLast Name:/STRONGBR
!-- Reprint the info on next page --
? echo $lastname; ?/p
PSTRONGFirst Name:/STRONGBR
? echo $firstname; ?/P
PSTRONGEmail:/STRONGBR
? echo $email; ?/P
/TD
!-- repeat --
Pa href=show_addrecord3.htmlClick here to add another./a/p
/TD
/TR
/TABLE
/BODY
/HTML
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php