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>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>

<H1>Adding a Record to tbltest</H1>

<FORM METHOD="post" ACTION="do_addrecord3.php">

<TABLE CELLSPACING=3 CELLPADDING=3>
<TR>
<TD VALIGN=TOP>
<P>First Name<BR>
<INPUT TYPE="text" NAME="firstname" SIZE=30 MAXLENGTH=225></p>

<P>Last Name<BR>
<INPUT TYPE="text" NAME="lastname" SIZE=30 MAXLENGTH=225></P>

<P>Email<BR>
<INPUT TYPE="text" NAME="email" SIZE=30 MAXLENGTH=225></P>

</TD>


<TR>
<TD VALIGN=TOP COLSPAN=2 ALIGN=CENTER>
        </P>

<P><INPUT 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", "xxxxxx")
 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>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>

<H1>Adding a Record to <? echo "$table_name"; ?></H1>

<TABLE CELLSPACING=3 CELLPADDING=3>
<TR>
<TD VALIGN=TOP>
<P><STRONG>Last Name:</STRONG><BR>
<!-- Reprint the info on next page -->
<? echo "$lastname"; ?></p>

      <P><STRONG>First Name:</STRONG><BR>
<? echo "$firstname"; ?></P>

<P><STRONG>Email:</STRONG><BR>
<? echo "$email"; ?></P>
</TD>

<!-- repeat -->
<P><a href="show_addrecord3.html">Click 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=red>Please enter your name!</font><br>";
  $send = "no";
 }

 if ($lastname == "") {
  $lastname_err = "<font color=red>Please enter a message!</font><br>";
  $send = "no";
 }

 if ($email == "") {
  $email_err = "<font color=red>Please enter your e-mail
address!</font><br>";
  $send = "no";
 }


 if ($send != "no") {

/* Login to database */
$db_name = "drip_dripirrigationdb";
$table_name = "tbltest";

$connection = @mysql_connect("localhost", "drip", "33333333")
 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>
<TITLE>Add a Record</TITLE>
</HEAD>
<BODY>

<H1>Adding a Record to <? echo "$table_name"; ?></H1>

<TABLE CELLSPACING=3 CELLPADDING=3>
<TR>
<TD VALIGN=TOP>
<P><STRONG>Last Name:</STRONG><BR>
<!-- Reprint the info on next page -->
<? echo "$lastname"; ?></p>

      <P><STRONG>First Name:</STRONG><BR>
<? echo "$firstname"; ?></P>

<P><STRONG>Email:</STRONG><BR>
<? echo "$email"; ?></P>
</TD>

<!-- repeat -->
<P><a href="show_addrecord3.html">Click 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

Reply via email to