Hi All,
I was advised by another member to have some critique on my code to make it more efficient, and thought I'd get some critique on my code.
Here is the code from a page that I am using in a 2 page request form:
<?php
require( 'form.inc' );
// Declare Form Variables
$to = "example@example.com";
$subject = "Request Information from SHC Website";
$date = date( r ); //<-------- WHAT DO YOU EXPECT??? read the date function in php.net
$hostname = gethostbyaddr( $_SERVER['REMOTE_ADDR'] );
$connect = mysql_connect( $host, $user, $pass );
if ( !$connect )
{
die( "Could not connect to database: " . mysql_error() );
}
mysql_select_db( $db, $connect );
if(empty($_POST))
die("Form is empty!!!");
$_POST=array_map("mysql_real_escape_string",$_POST);
$email = $_POST['email'];
$cell = $_POST['cell'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$message = $_POST['message'];
$headers = 'From: ' . $email . "\r\n" . 'REPLY TO: ' . $email . "\r\n" . 'Date: ' . $date . "\r\n" . 'Sender Name: ' . $first . $last . "\r\n" . 'Sender Phone: ' . $phone . "\r\n" . 'Sender Cell: ' . $cell . "\r\n" . 'Sender Address: ' . $address . "\r\n" . 'Sender City: ' . $city . "\r\n" . 'Sender Zipcode: ' . $zip . "\r\n" . 'Area of Study: ' . $aoStudy . "\r\n" . 'Program of Interest: ' . $program . "\r\n" . 'Campus of Choice: ' . $campus . "\r\n" . 'Sender Hostname: ' . $hostname . "\r\n";
if ( empty($first ) || empty( $last ) || empty( $phone ) || empty( $campus ) || empty( $aoStudy ) || empty( $program ) || empty($email ) || empty( $cell ) || empty( $address ) || empty( $city ) || empty( $state ) || empty( $zip ) )
{
echo "<p class='intro'><span class='urgent'>ERROR</span> Information was not entered into required fields marked with a red asterisk (<span class='urgent'>*</span>. Please go back and resubmit the necessary information. We apologize for the inconvenience. Have a great day.</p>";
}
else
{
$insert = "INSERT INTO requests (fname, lname, phone, campus, aostudy, program, email, cell, address, city, state, zip) VALUES ('$first', '$last', '$phone', '$campus', '$aoStudy', '$program', '$email', '$cell', '$address', '$city', '$state', '$zip');";
/* UNDEFINED INDEXES IN YOUR CODE */
if ( !mysql_query( $insert, $connect ) )
{
die( "Error: " . mysql_error() );
}
mysql_close( $connect );
if ( mail( $to, $subject, $message, $headers ) )
{
echo "<p class='intro'>The e-mail message has been sent. We will reply to your inquiry as soon as possible. Thank you, and have a great day. <br />
<a href='home.html'>Click here to return to homepage.</a></p>";
echo "Dated $date";
}
else
{
echo "<p class ='intro'>The e-mail could not be delivered to $email. Please<a href='contact.html'> return to the previous page</a> to try to resend the message. If you have confirmed that you are entering in the correct information, and are seeing this message more than once, please notify us at example@example.com. Thank you, and have a great day.</p>";
echo "Dated $date";
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
</body>
</html>
Thank you for your critique and input.