lundi 30 mars 2015

Undefined variable within email form (message variable)

I'm building a website based on bootstrap 3, and I require it to have a contact form. The code is literally copy and pasted but have adapted it for my use. It's quite simple and unfortunately, I cannot find the reference I used for it. below is the extract of the HTML code



<div class="container">
<div class="row">
<div class="col-md-4">
<h3>Message</h3><hr>
<form name="contactform" method="post" action="js/contact.php">
<div class="input-group">
<input type="text" class="form-control" placeholder="Name" aria-describedby="name">
</div>

<br>

<div class="input-group">
<input type="email" class="form-control" placeholder="Email" aria-describedby="email">
</div>

<br>

<div class="input-group">
<input type="tel" class="form-control" placeholder="Phone" aria-describedby="phone">
</div>

<br>

<div class="input-group">
<input type="text" class="form-control" placeholder="Message" aria-describedby="message">
</div>

<br>

<div class="input-group-btn">
<input class="btn btn-default" type="submit" value="Submit"><a href="js/contact.php"></a>
</div>

</form>
</div>


The Following below is the PHP script that was used;



<?php

if(isset($_POST['email'])) {



// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "fullbodyfitness@outlook.com";

$email_subject = "enquiry";





function died($error) {

// your error code can go here

echo "We are very sorry, but there were error(s) found with the form you submitted. ";

echo "These errors appear below.<br /><br />";

echo $error."<br /><br />";

echo "Please go back and fix these errors.<br /><br />";

die();

}



// validation expected data exists

if(!isset($_POST['name']) ||

!isset($_POST['email']) ||

!isset($_POST['phone']) ||

!isset($_POST['message'])) {

died('We are sorry, but there appears to be a problem with the form you submitted.');

}



$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['phone']; // not required

$comments = $_POST['message']; // required



$error_message = "";

$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {

$error_message .= 'The Email Address you entered does not appear to be valid.<br />';

}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$name)) {

$error_message .= 'The Name you entered does not appear to be valid.<br />';

}

}

if(strlen($message) < 2) {

$error_message .= 'The Message you entered do not appear to be valid.<br />';

}

if(strlen($error_message) > 0) {

died($error_message);

}

$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: ".clean_string($email_from)."\n";

$email_message .= "Phone: ".clean_string($phone)."\n";

$email_message .= "Message: ".clean_string($message)."\n";





// create email headers

$headers = 'From: '.$email_from."\r\n".

'Reply-To: '.$email_from."\r\n" .

'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);

?>



<!-- include your own success html here -->



Thank you for contacting us. We will be in touch with you very soon.


}

<?php


?>


If there are any questions, please ask. I hope I have provided enough detail, any help is appreciated! Thank you!!!


Aucun commentaire:

Enregistrer un commentaire