To keep the PHP and HTML pages separate is easy. Just use the
require_once or
include_once PHP function and convert the file to a .php file. It will be one script, but different files.
Sendmail.php
PHP Code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "email@email.com";
$email_subject = "Express Travel Booking";
$Name = $_POST['Name']; // required
$email_from = $_POST['email']; // required
$Telephone = $_POST['Telephone']; // not required
$PickUp = $_POST['PickUp']; // required
$DropOff = $_POST['DropOff']; // required
$Passengers = $_POST['Passengers']; // required
$Date = $_POST['Date']; // required
$Time = $_POST['Time']; // required
$Comments = $_POST['Comments']; // required
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name : ".clean_string($Name)."\n";
$email_message .= "email address : ".clean_string($email_from)."\n";
$email_message .= "Telephone : ".clean_string($Telephone)."\n";
$email_message .= "Pick Up : ".clean_string($PickUp)."\n";
$email_message .= "Drop Off : ".clean_string($DropOff)."\n";
$email_message .= "Passengers : ".clean_string($Passengers)."\n";
$email_message .= "Date : ".clean_string($Date)."\n";
$email_message .= "Time : ".clean_string($Time)."\n\n";
$email_message .= "Comments : \n\n".clean_string($Comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(@mail($email_to, $email_subject, $email_message, $headers))
{
//if email sending is successful
//Note the below message variable. It will be echoed at the bottom of the form
$complete_message = 'Thank you for your enquiry. We will be in touch soon.';
}
}
?>
So, form.php will be :
PHP Code:
<?php
require_once('sendmail.php');
//Same as copy-pasting sendmail.php code at this point
?>
<form method="post">
<fieldset>
<legend>Contact Form</legend>
<div class="Form">
<label for="Name">Name</label><input type="text" class="inset" name="Name" id="Name" accesskey="1" title="enter name" />
<label for="email">email address</label><input type="text" class="inset" name="email" id="email" accesskey="2" title="enter email address" />
<label for="Telephone">Telephone</label><input type="text" class="inset" name="Telephone" id="Telephone" accesskey="3" title="enter Telephone Number" />
<label for="PickUp">Pick Up</label><input type="text" class="inset" name="PickUp" id="PickUp" accesskey="4" title="enter Pick Up Destination" />
<label for="DropOff">Drop Off</label><input type="text" class="inset" name="DropOff" id="DropOff" accesskey="5" title="enter Drop Off Destination" />
<label for="Passengers">Passengers</label><input type="text" class="inset" name="Passengers" id="Passengers" accesskey="5" title="enter number of Passengers" />
<label for="Date">Date</label><input type="text" class="inset" name="Date" id="Date" accesskey="6" title="enter Date" />
<label for="Time">Time</label><input type="text" class="inset" name="Time" id="Time" accesskey="7" title="enter Time" />
<label for="Comments">Comments</label>
<textarea id="Comments" name="Comments" class="comment"></textarea>
<div class="submit"><input name="Submit" type="submit" class="postc" value="Send"></div>
<div class="submit"><input name="Reset" type="reset" class="reset" value="Reset"/></div>
</div>
</fieldset>
</form>
<?php
echo $complete_message; //The thank you message will be echoed if the emailing is successful in sendmail.php
?>
By
leaving out the form action attribute completely, the form will be submitted to the same page where it will be processed by the sendmail.php code.