I am trying to get a PHP form to email me the input fields but I am very new to PHP and don't understand why this isn't working. When Submit is clicked it displays the PHP file as a text file in the web browser. How do I fix this? Thanks in advance. Here is the PHP as it sits. I can upload the HTML and relevant CSS if needed, but don't know why they would be. Also, in case it is relevant, the file extension for the PHP is PHP. I'm pretty sure that's what it is supposed to be.
Do to a few comments I reduced the name of the variables and removed the special characters. Here is the updated code.
Thanks for the answers and links. I checked them all out and discovered that in the end this was a total noob mistake. I hadn't downloaded any php software so my computer want translating the code from the local source. I'm downloading wamp when I get home. That should hopefully fix things.
if(isset($_POST['email'])) {
$email_to = "sellit4223@gmail.com";
$email_subject = "GOT ONE";
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['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['business_or_product_name']) ||
!isset($_POST['Doing']) ||
!isset($_POST['Do'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // required
$business_or_product_name = $_POST['business_or_product_name']; // required
$Doing = $_POST['Doing']; // not required
$Do = $_POST['Do']; // not 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,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$business_or_product_name)) {
$error_message .= 'The business or product name you entered does not appear to be valid.<br />';
}
if(strlen($Doing) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($Do) < 2) {
$error_message .= 'The Comments 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 .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "business_or_product_name: ".clean_string($business_or_product_name)."\n";
$email_message .= "Doing: ".clean_string($Doing)."\n";
$email_message .= "Do: ".clean_string($Do)."\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
}
?>