Contact Form
Source Code
The Source file is named 'contact.txt' in the distribution zip file.
<link href="forms.css" rel="stylesheet" type="text/css" media="all" />
<?php
// move these variables to a cfg.ini file included at the head of this file???
$site_email = 'jlhaslip@yahoo.ca';
$site_name = 'Web Site Template Distibution File';
if (isset($_POST['posted'])) {
$email = trim($_POST['email']);
$f_name = trim($_POST['first_name']);
$l_name = trim($_POST['last_name']);
$body = trim($_POST['note_wide']) ;
$date = trim(date("D M j G:i:s T Y"));
$name = $f_name . ' ' . $l_name;
$theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email, $trashed);
if ( $f_name == "" || $f_name == " ") {
echo '<p><span class="border bold red">A first name is required</span></p>';
}
elseif ( $l_name == "" || $l_name == " " ) {
echo '<p><span class="border bold red">A last name is required</span></p>';
}
elseif ( $email == "" || $email == " " ) {
echo '<p><span class="border bold red">An email address is required</span></p>';
}
elseif (!$theresults) {
echo '<p><span class="border bold red">The email address must be in a valid form similar to " your_name@example.com ".</span></p>';
}
elseif ( $body == "" || $body == " " ) {
echo '<p><span class="border bold red">A message body is required</span></p>';
}
}
if ( $f_name && $l_name && $email && $body) {
$file_name = "message_data.txt";
// Error Supress the fopen
$handle = @fopen($file_name, "a");
// Check for successful fopen
if (!$handle) {
// error message and exit if no file handle available
echo "File Handle Not Available For Use"; exit;
}
/*
commented out to run on a windows machine since windows does not recognize chmod
if (!is_writeable($file_name) {
chmod($filename, 0666);
}
remove the comments to run on a unix/linux machine that recognizes the chmod function
*/
$body = strip_tags(str_replace( "\r\n" , ' ' , $body ));
$body = strip_tags(str_replace( ',' , '' , $body ));
$message_string = $date . ' , ' . $name . ' , ' . $email . ',' . $body . ',' . $_SERVER['REMOTE_ADDR']."\r\n";
fwrite($handle, $message_string);
//close the open file prior to ending
fclose($handle);
if (@mail( $site_email, $site_name, $body,"From: $email <$name>\r\n")) {
echo '<p class="border"><span class="bold red">Thank you for submitting this message. </span><br /><br /><span class="bold">Your comments and requests are important to us. If a reply is required, you can expect to hear from us shortly.</span><br /><br /><span class="bold red">Thanks again.</span> Select from the Menu to navigate to another page, or <a href="index.html" title=" Return to the site Home Page"> here </a> to reurn to the Index page..</p>';
unset($_POST);$_POST['posted'] = 'false';
}
else {
echo "<p class=\"border red\">Message failed to be accepted by the Mail system. <br /><span class=\"bold\">Please send the message to $site_email from your own email program.</span> <br />The site has been sent a report of the mail system failure, but not the contents of the message. <br />We will endevour to correct the problem immediately.</p>";
unset($_POST);$_POST['posted'] = 'false';
}
};
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post" enctype="multipart/form-data">
<?php if (!isset($_POST['posted'])) { echo '<p><strong>All</strong> fields are required.</p>';} ?>
<fieldset><legend>Personal Information</legend>
<div class="notes">
<h4>Personal Information</h4>
<p class="last">Please enter your name and a valid email address, then your message.<br />
We will never sell or disclose your email address to anyone. </p>
</div>
<div class="required">
<label for="first_name">First Name:</label>
<input type="text" name="first_name" id="first_name" class="inputText" size="20" maxlength="40" value="<?php echo $_POST['first_name'] ?>" />
</div>
<div class="hidden">
<input type="hidden" name="posted" value="true" />
</div>
<div class="required">
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="last_name" class="inputText" size="20" maxlength="40" value="<?php echo $_POST['last_name'] ?>" />
</div>
<div class="required">
<label for="email">Email:</label>
<input type="text" name="email" id="email" class="inputText" size="40" maxlength="100" value="<?php echo $_POST['email'] ?>" />
</div>
<fieldset>
<div class="required wide">
<label for="note_wide">Your Message:</label>
<textarea name="note_wide" id="note_wide" class="inputTextarea" rows="6" cols="55"><?php echo $_POST['note_wide'] ?></textarea>
<br />
<small>We would love to get your feedback or any comments on your experience with us.</small>
</div>
</fieldset>
<fieldset>
<div class="submit">
<div>
<input type="submit" class="inputSubmit" value="Submit »" />
<input type="reset" class="inputSubmit" value="Reset Form" />
</div>
</div>
</fieldset>
</form>
</div>
How It Works
Process the Information
If this form has been submitted as a 'posted' form ie: the user clicked Submit, then there is a bunch of error checking to confirm there are no blanks Input fields and the email address is 'valid form'. It does no confirmation of the existance of the address, only confirms the 'form' of the address, then it submits a fully completed form for emailing to the site address. The site address is the variable named '$site_email' and will need to be modified for every installation of this script unless you want the emails to come to me. (which I prefer not to happen) Also, modify the variable named '$site_name' since this is used as the 'Subject' of the emails. The only other things which need to be done is: 1.) 'chmod' the file named 'message_data.txt' (used for the logging of emails sent through the form) to '666' so that the file can be written to. 2.) uncomment the lines of the script which makes reference to the chmod function. Only un-comment if you are running this file on anything but Windows. Windows does not recognize the chmod function, and the file must allow being written to.
// move these variables to a cfg.ini file included at the head of this file???
$site_email = 'your_name@someplace.com';
$site_name = 'Web Site Contact Form';
if (isset($_POST['posted'])) {
$email = trim($_POST['email']);
$f_name = trim($_POST['first_name']);
$l_name = trim($_POST['last_name']);
$body = trim($_POST['note_wide']) ;
$date = trim(date("D M j G:i:s T Y"));
$name = $f_name . ' ' . $l_name;
$theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $email, $trashed);
if ( $f_name == "" || $f_name == " ") {
echo '<p><span class="border bold red">A first name is
required</span></p>';
}
elseif ( $l_name == "" || $l_name == " " ) {
echo '<p><span class="border bold red">A last name is
required</span></p>';
}
elseif ( $email == "" || $email == " " ) {
echo '<p><span class="border bold red">An email address
is required</span></p>';
}
elseif (!$theresults) {
echo '<p><span class="border bold red">The email
address must be in a valid form similar to " your_name@
example.com ".</span></p>';
}
elseif ( $body == "" || $body == " " ) {
echo '<p><span class="border bold red">A message body
is required</span></p>';
}
}
if ( $f_name && $l_name && $email && $body) {
$file_name = "message_data.txt";
// Error Supress the fopen
$handle = @fopen($file_name, "a");
// Check for successful fopen
if (!$handle) {
// error message and exit if no file handle available
echo "File Handle Not Available For Use"; exit;
}
/*
commented out to run on a windows machine since windows does not recognize chmod
if (!is_writeable($file_name) {
chmod($filename, 0666);
}
remove the comments to run on a unix/linux machine that recognizes the chmod function
*/
$body = strip_tags(str_replace( "\r\n" , ' ' , $body ));
$body = strip_tags(str_replace( ',' , '' , $body ));
$message_string = $date . ' , ' . $name . ' , ' . $email . ',' . $body . ','
. $_SERVER['REMOTE_ADDR']."\r\n";
fwrite($handle, $message_string);
//close the open file prior to ending
fclose($handle);
if (@mail( $site_email, $site_name, $body,"From: $email <$name>\r\n")) {
echo '<p class="border"><span class="bold red">Thank you for
submitting this message. </span><br /><br /><span
class="bold">Your comments and requests are important to us.
If a reply is required, you can expect to hear from us shortly.
</span><br /><br /><span class="bold red">
Thanks again.</span> Select from the Menu to navigate to another page,
or <a href="index.html" title=" Return to the site Home Page"> here
</a> to reurn to the Index page..</p>';
unset($_POST);$_POST['posted'] = 'false';
}
else {
echo "<p class=\"border red\">Message failed to be accepted by the Mail system. <br /><span class=\"bold\">Please send the message to $site_email from your own email program.</span> <br />The site has been sent a report of the mail system failure, but not the contents of the message. <br />We will endevour to correct the problem immediately.</p>";
unset($_POST);$_POST['posted'] = 'false';
}
};
The Displayed Form
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"
enctype="multipart/form-data">
<?php if (!isset($_POST['posted'])) { echo '<p><strong>All
</strong> fields are required.</p>';} ?>
<fieldset><legend>Personal Information</legend>
<div class="notes">
<h4>Personal Information</h4>
<p class="last">Please enter your name and a valid email address,
then your message.<br />
We will never sell or disclose your email address to anyone. </p>
</div>
<div class="required">
<label for="first_name">First Name:</label>
<input type="text" name="first_name" id="first_name"
class="inputText" size="20" maxlength="40" value="<?php echo
$_POST['first_name'] ?>" />
</div>
<div class="hidden">
<input type="hidden" name="posted" value="true" />
</div>
<div class="required">
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" id="last_name"
class="inputText" size="20" maxlength="40" value="<?php
echo $_POST['last_name'] ?>" />
</div>
<div class="required">
<label for="email">Email:</label>
<input type="text" name="email" id="email"
class="inputText" size="40" maxlength="100" value="<?php
echo $_POST['email'] ?>" />
</div>
<fieldset>
<div class="required wide">
<label for="note_wide">Your Message:</label>
<textarea name="note_wide" id="note_wide" class="inputTextarea"
rows="6" cols="55"><?php echo $_POST['note_wide'] ?></textarea>
<br />
<small>We would love to get your feedback or any comments
on your experience with us.</small>
</div>
</fieldset>
<fieldset>
<div class="submit">
<div>
<input type="submit" class="inputSubmit" value="Submit »" />
<input type="reset" class="inputSubmit" value="Reset Form" />
</div>
</div>
</fieldset>
</form>
</div>
Check Your Data
The same Data Checking script used to check the Menu Data should be able to check the data for the email data file. Only thing is to modify the file referenced in the script and the results should list the emails sent rather than the Menu data. This Template uses 'message_data.txt' by default to Log the emails sent, so unless you have changed the data file reference, that is the name which should be checked.
Previous Page : The Menu Data StructureNext Page: The Template Index page