Jump to content

Mandatory email field on a Contact page


random1739

Recommended Posts

Hi everyone

 

I've used Smarty to make a PHP-powered Contact page on a web site. At the moment it works fine and displays fine, but I'd like to redirect the user to an error page if they forget to include their return email address.

 

Here's the complete code from the PHP page:

<?php

require_once('libs/Smarty.class.php');
$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;

if (isset($_POST['action']) && $_POST['action'] == 'Submit request')
{
$body = '';
$body .= 'How did you hear about Dive Adventures? ' . $_POST['heard'] . "\n";
$body .= "\n";
$body .= '* Your adventure' . "\n";
$body .= 'Which destination are you interested in? ' . $_POST['holiday'] . "\n";
$body .= 'When do you intend to travel? ' . $_POST['month'] . ' ' . $_POST['year'] . "\n";
$body .= 'What type of diving do you enjoy doing? ' . $_POST['diving_type'] . "\n";
$body .= 'Number of guests: ' . "\n";
$body .= 'Adults: ' . $_POST['adults'] . ' (' . $_POST['adult_divers'] . ' are divers)' . "\n";
$body .= 'Children: ' . $_POST['children'] . ' (' . $_POST['child_divers'] . ' are divers)' . "\n";
$body .= 'Infants: ' . $_POST['infants'] . ' (' . $_POST['infant_divers'] . ' are divers)' . "\n";
$body .= 'Comments: ' . "\n";
$body .= $_POST['comments'] . '.' . "\n";
$body .= "\n";
$body .= '* Your details' . "\n";
$body .= 'Name ' . $_POST['name'] . "\n";
$body .= 'Email ' . $_POST['email'] . "\n";
$body .= 'Address ' . $_POST['address'] . "\n";
$body .= 'City ' . $_POST['city']  . "\n";
$body .= 'Postcode ' . $_POST['postcode']  . "\n";
$body .= 'State ' . $_POST['state']  . "\n";
$body .= 'Country ' . $_POST['country']  . "\n";
$body .= 'Contact by telephone? ' . ($_POST['contactbytelephone'] ? 'Yes' : 'No' ) . "\n";
$body .= 'Telephone number ' . $_POST['phone'] . "\n";
$body .= 'Monthly newsletter? ' . ($_POST['receivenewsletter'] ? 'Yes' : 'No' ) . "\n";
//echo str_replace("\n", '<br>', $body);

$to = 'about@diveadvntures.';
$from = "diveadventures <website@diveadvntures.com.au>";
$subject = "Holiday information - From (URL address blocked: See forum rules)";
$headers = "From: $from";
mail($to, $subject, $body, $headers);

$smarty->assign('submit', 1);

}


smarty_constants(&$smarty);

$smarty->display('head.tpl');
$smarty->display('contact.tpl');
$smarty->display('tail.tpl');


function smarty_constants(&$smarty)
{
$HEARDS = array (
	'The Internet' => 'The Internet',
	'Word of Mouth' => 'Word of Mouth',
	'Business reference' => 'Business reference',
	'Advertisement' => 'Advertisement',
	'Dive magazine' => 'Dive magazine',
	'Other' => 'Other'
);

$smarty->assign('heards', $HEARDS);


$HOLIDAYS = array (
	'Australia / Great Barrier Reef' => 'Australia / Great Barrier Reef',
	'Cocos (Keeling) / Christmas Islands' => 'Cocos (Keeling) / Christmas Islands',
	'Cook Islands' => 'Cook Islands',
	'East Timor' => 'East Timor',
	'Fiji' => 'Fiji',
	'Indonesia' => 'Indonesia',
	'Liveaboards' => 'Liveaboards',
	'Malaysia' => 'Malaysia',
	'Maldives' => 'Maldives',
	'Marshall Islands' => 'Marshall Islands',
	'Micronesia' => 'Micronesia',
	'Niue' => 'Niue',
	'Papua New Guinea' => 'Papua New Guinea',
	'Philippines' => 'Philippines',
	'Red Sea / Egypt' => 'Red Sea / Egypt',
	'Solomon Islands' => 'Solomon Islands',
	'Tahiti' => 'Tahiti',		
	'Tonga' => 'Tonga',		
	'Vanuatu' => 'Vanuatu',		
	'Western Samoa' => 'Western Samoa',		
	'Other (Refer to comments)' => 'Other (Refer to comments)'

);

$smarty->assign('holidays', $HOLIDAYS);


$DIVING_TYPES = array (
	'Wreck' => 'Wreck',
	'Cave' => 'Cave',
	'Reef' => 'Reef',
	'Wall' => 'Wall',
	'Temperate Water' => 'Temperate Water',
	'Tropical' => 'Tropical',
	'Deep' => 'Deep',
	'Drift' => 'Drift',
	'Exploratory' => 'Exploratory',
	'Live-aboards' => 'Live-aboards',
	'Technical Diving' => 'Technical Diving',
	'Mixed Gases' => 'Mixed Gases'
);

$smarty->assign('diving_types', $DIVING_TYPES);


$MONTHS = array (
	'January' => 'January',
	'February' => 'February',
	'March' => 'March',
	'April' => 'April',
	'May' => 'May',
	'June' => 'June',
	'July' => 'July',
	'August' => 'August',
	'September' => 'September',
	'October' => 'October',
	'November' => 'November',
	'December' => 'December'
);

$smarty->assign('months', $MONTHS);

$YEARS = array (
	'2007' => '2007',
	'2008' => '2008',
	'2009' => '2009',
	'2010' => '2010',
);

$smarty->assign('years', $YEARS);

return;
}

?>

Link to comment
Share on other sites

Here is my contact page that redirects he user back to the contact page and tells them what feilds are missing

 

<?
// Change to your own email address
$your_email = "youremail@yourdomain.com";

// This is what is displayed in the email subject line
// Change it if you want
$subject = "Message via your contact form";

// This is displayed when the email has been sent
$thankyou_message = "<p>Thankyou. Your message has been sent.</p>";

$self = $_SERVER['REQUEST_URI'];

$name = $_POST['txtName'];
$email = $_POST['txtEmail'];
$message = $_POST['txtMessage'];
$send = $_POST['send'];

$msg="<p>Please fill in this form if you have any queries or suggestions.</p>";

echo ($msg);

$form = "
    <form method=\"post\" action=\"$self\">

    <p><label for=\"txtName\">Name:</label>
    <input type=\"text\" title=\"Please enter your name\" id=\"txtName\" name=\"txtName\" size=\"40\" value=\"$name\" /></p>

    <p><label for=\"txtEmail\">Email:</label>
    <input type=\"text\" title=\"Please enter your email address\" id=\"txtEmail\" name=\"txtEmail\" size=\"40\" value=\"$email\"/></p>

    <p><label for=\"txtMessage\">Comments:</label>
    <textarea title=\"Please enter your message\" id=\"txtMessage\" name=\"txtMessage\" rows=\"20\" cols=\"45\">$message</textarea></p>

    <p><label> </label>
    <input type=\"submit\" class=\"sendbutton\" name=\"send\" value=\"Submit\" /></p>

</form>";

if($send)
{
    $valid=true;

if( !$name )
{ 
    $errmsg.="Please enter your name:<br />";
    $valid=false;
}

if( !$email )
{
    $errmsg.="Please enter your email address:<br />";
    $valid=false;
}
else
{
	$email = trim($email);
	$_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+";
	$_host = "([-0-9A-Z]+\.)+";
	$_tlds = "([0-9A-Z]){2,4}$/i";

	if( !preg_match($_name."@".$_host.$_tlds,$email))
	{
		$errmsg.="Email address has incorrect format!<br />";
		$valid=false;
	}
}

if( !$message )
{ 
    $errmsg.="Please enter your message:<br />";
    $valid=false; 
}
}

if( $valid !=true )
    {
echo( "<span style=\"font-weight: bold; color:red;\">".$errmsg."</span>" . $form );
    }

else {

    // Stop the form being used from an external URL
    // Get the referring URL
    $referer = $_SERVER['HTTP_REFERER'];
    // Get the URL of this page
    $this_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
    // If the referring URL and the URL of this page don't match then
    // display a message and don't send the email.
    if ($referer != $this_url) {
        echo "You do not have permission to use this script from another URL.<br />";
echo "If you are behind a firewall please check your referrer settings.";
        exit;
    }

    // The URLs matched so send the email
    if( mail($your_email, $subject, $message, "From: $name <$email>"));
    {
    	// Display the thankyou message
    	echo $thankyou_message;
    }
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.