Email contact form with response in the same page - php

i've this email contact form that works well but open me the response in a blank empty page. I would the response in the same page. Can anyone help me? Thank you in advance.
<?php
// Site Info
$site_name = 'xxx';
$site_email = 'info#sitename.com';
if(isset($_POST['contact_email'])){
$contact_name = $_POST['contact_name'];
$contact_phone = $_POST['contact_phone'];
$contact_email = $_POST['contact_email'];
$contact_subject = $_POST['contact_subject'];
$contact_message = $_POST['contact_message'];
$from_email = $contact_email;
$contact_name = "Name: $contact_name <br />";
$contact_email = "Email: $contact_email <br />";
$contact_phone = "Phone Number: $contact_phone <br />";
$contact_subject = "Subject: $contact_subject <br />";
$contact_message = "Message: $contact_message <br />";
$to = $site_email;
$subject = "You have a new email from ".$site_name;
$header = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$header .= 'From:'.$from_email. " \r\n";
$message = "
You have a new message! <br />
$contact_name
$contact_email
$contact_phone
$contact_subject
$contact_message
";
// Send Mail
if(#mail($to,$subject,$message,$header)) {
$send = true;
} else {
$send = false;
}
if(isset($_POST['ajax'])){
if($send)
echo 'success';
else
echo 'error';
}
}
HTML
<form id="contact_form" name="contact_form" method="post" action="page_contact_ajax.php" >
<div class="form-row field_text">
<label for="contact_name">Your Name </label>
<em>(required)</em><br>
<input id="contact_name" class="input_text required" type="text" value="" name ="contact_name">
</div>
<div class="form-row field_text">
<label for="contact_phone">Your Phone Number </label><em>(optional)</em><br>
<input id="contact_phone" class="input_text" type="text" value="" name ="contact_phone">
</div>
<div class="form-row field_text">
<label for="contact_email">Your E-Mail Address </label><em>(required)</em><br>
<input id="contact_email" class="input_text required" type="text" value="" name ="contact_email">
</div>
<div class="form-row field_text">
<label for="contact_subject">Subject </label><em>(required)</em><br>
<input id="contact_subject" class="input_text required" type="text" value="" name ="contact_subject">
</div>
<div class="form-row field_textarea">
<label for="contact_message">Message: </label><br>
<textarea id="contact_message" class="input_textarea" type="text" value="" name ="contact_message"></textarea>
</div>
<div class="form-row field_submit">
<input type="submit" value="Submit Now" id="contact_submit" class="ui button colored">
<span class="loading hide"><img src="assets/images/preloader.gif"></span>
</div>
<div class="form-row notice_bar">
<p class="notice notice_ok hide">Thank you for contacting us. We will get back to you as soon as possible.</p>
<p class="notice notice_error hide">Due to an unknown error, your form was not submitted. Please resubmit it or try later.</p>
</div>
</form>
HTML
Your Name
(required)
Your Phone Number (optional)
Your E-Mail Address (required)
Subject (required)
Message:
Thank you for contacting us. We will get back to you as soon as possible.
Due to an unknown error, your form was not submitted. Please resubmit it or try later.

PHP Manual says replacing \r\n with \n works in some cases.
Yon could remove # from #mail(...) and use error_get_last() to see what's wrong with mail. See this answer.

Related

HTML Post action sends me to php form

I am a complete novice with PHP, I just want a simple email contact form that will display an error if a required field isn't entered, and will give a thank you message if the email is sent.
When I click my submit button the email does get sent but the website redirects straight to the /send-email.php page, which is blank and useless. The required form fields also don't seem to do anything in the way of preventing an email from being sent if the forms aren't filled.
HTML form code:
<form class="contact-form" action="php/send-email.php" method="post" novalidate>
<div class="row">
<div class="column width-6 pad-1 contact-column">
<h5>Your Name*</h5>
<input type="text" name="name" class="form-box" tabindex="1" required>
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Email*</h5>
<input type="text" name="email" class="form-box" tabindex="2" required>
</div>
</div>
<div class="row mt-2">
<div class="column width-6 pad-1 contact-column">
<h5>Your Website</h5>
<input type="text" name="website" class="form-box" tabindex="3">
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Company</h5>
<input type="text" name="company" class="form-box" tabindex="4">
</div>
</div>
<div class="column width-12">
<input type="text" name="honeypot" class="form-honeypot">
</div>
<div class="row mt-2">
<div class="column width-12 pad-1">
<h5>Your Message*</h5>
<textarea name="message" class="form-text" tabindex="5" required></textarea>
</div>
</div>
<div class="row mt-2">
<input type="submit" value="Send Your Message!" class="btn btn-large bg-blue">
</div>
</form>
Php file
<?php
$recipient = "contact#mysite.com";
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$website = filter_var($_POST["website"], FILTER_SANITIZE_URL);
$company = filter_var($_POST["company"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$headers .= 'Reply-To: '.$email.'' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$subject = "New email from contact form";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$email_content .= "Website: $website\n";
$email_content .= "Company: $company\n\n";
$email_content .= "Message:\n$message\n\n\n";
if(mail($recipient, $subject, $email_content, $headers)){
echo "Thanks for the email, we'll get back to as soon as possible!";
}
?>
Any help in the right direction is appreciated!
Check for empty input. Try using if else statement. eg.
if($name == '')
{
echo "Please fill in name";
}
else if($var == '')
{
//error message
}
or you can try
if($name == '' || $email == '' || $var == '')
{
echo "Please fill in all the blank";
}
I'm quite new too to PHP, so this is just a simple checking. Hope it'll helps!

Troubleshooting php form submition blank fields in emails

I know this question has been asked before, I have researched but can't seem to get to the route of the problem. I'm hoping someone can help. Ok so form seems to be working in as much as the form has to be completed before it allows to be sent, the form sends and an email arrives. Unfortunately the email fields are blank.
So here's the php:
<?php
$Body = 'MIME-Version: 1.0' . "\r\n";
$Body .= 'Content-type: text/html; ch***t=utf-8' . "\r\n";
$Body .= 'From: '. $EmailFrom . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$EmailFrom = "info#mywebsite.com";
$EmailTo = "info#mywebsite.com";
$Subject = "Contact Form";
$Name = trim(stripslashes($_POST['Name']));
$Email = trim(stripslashes($_POST['Email']));
$Message = trim(stripslashes($_POST['Message']));
// validation
$validationOK=true;
if (!$validationOK) {
echo "please check your details";
header("Location: http://mywebsite.com/formredirect.html");
exit;
}
// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";
// send email
$success = mail($EmailTo, $Subject, $Body, "From: $EmailFrom" . "\r\n");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"1;URL=formredirect.htm\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"1;URL=error.html\">";
}
?>
My HTML:
<form method="post" action="contact.php" name="contact" id="contact">
<div class="row 50%">
<div class="6u 12u(mobile)">
<input type="text" name="name" id="name" placeholder="Name" required />
</div>
<div class="6u 12u(mobile)">
<input type="text" name="email" id="email" placeholder="Email" required />
</div>
</div>
<div class="row 50%">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" required></textarea>
</div>
</div>
<div class="row 50%">
<div class="12u">
<input type="submit" class="button" id="submit" value="submit"></a>
</div>
</div>
</form>
I appreciate it's probably me being really dense. I'm new to this so am fumbling my way around. Any help would be greatly appreciated. Many thanks!
The $_POST keys are CaSe SenSitIve
$_POST['Name'] is different from $_POST['name']
Either change the form name attribute or the POST keys so they match, i.e.:
FORM
<input type="text" name="name" id="name" placeholder="Name" required />
...
<input type="text" name="email" id="email" placeholder="Email" required />
...
<textarea name="message" id="message" placeholder="Message" required></textarea>
...
PHP
...
$Name = trim(stripslashes($_POST['name']));
$Email = trim(stripslashes($_POST['email']));
$Message = trim(stripslashes($_POST['message']));
...

PHP Undefined index error with form

I keep getting an error as follows when i submit my form. I'm not sure what it is. I'm running the site on WAMP (windows) and im writing it in dreamweaver CS6. Please note I'm fairly new to PHP. Thanks!
Here is the code:
Notice: Undefined index: message in S:\wamp\www\latech\contact.php on line 29
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4>Go back and try again';
exit;
}
// Add the recipient email to a variable
$to = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4>Go back and try again';
exit;
}
// Add the recipient email to a variable
$to = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>
Your name attribute is msg your calling it message in PHP.
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
$msg = $_POST['message']; // no need to trim message
Also here...
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
The first occurrence should be $message = because there is nothing to concatenate when it hasn't been initialized.
You are using
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
Rather use
<textarea id="msg" name="message" spellcheck="true" ></textarea>

HTML form with external php form processing?

Below I have my html and php coding. I've racked my brain trying several different options, but what I am really looking for is something simple and secure that either reloads to my original page showing sent message. As of right now I get this error message when submitting my form. Someone please point me in the right direction, any feedback is appreciated.
HTML CODING(index.html)
<html>
<head><title></title></head>
<body>
<div id="wrapper2">
<div id="newsletter" class="container">
<div class="title">
<img src="images/port-formdivide.png" alt="" width="800" height="60" />
<h2>Contact Me</h2>
<span class="byline">Please complete the entire form.</span> </div>
<div class="content">
<form action="form.php" method="POST" enctype="multipart/form-data" target="_self">
<div class="row half">
<div class="6u">
<input name="name/company" id="name/company" type="text" class="text" maxlength="30"
placeholder="Name/Company" value="" />
</div>
<div class="6u">
<input name="email" id="email" type="text" class="text" maxlength="30"
placeholder="Email" value="" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" value=""></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input name="Send Message" type="submit" class="button submit" value="Send Message" />
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
PHP CODING(form.php)
<?php
// Email Variables
$to = 'inquiries#triston-thomas.com';
$subject = 'New Inquiry';
/* Data Variables */
if ($_POST['parse_var'] == "form"){
$emailTitle = 'New Message Triston';
$yourEmail = 'inquiries#triston-thomas.com':
$name/company = $_POST['name/company'];
$email = $_POST['email'];
$message = $_POST['message'];
echo ("Name/Company:" . $name/company);
echo ("<br />Email:" . $email);
echo ("<br />Message:" . $message);
function response($name/company);
{
echo ("Thank You " . $name/company);
}
header ("Location: index.html");
mail ($to, $subject, $message, "From: ".$name/company);
echo "Your Inquiry Has Been Sent";
// Set Required Fields
$required_ fields = array("$name/company","$email","$message");
// Set Error Messages
$error_message = array("$name/company" => 'Please Enter Your Name or Company Name In
The Field',
"$email" => 'Valid Email Address Is Required',
"$message" => 'Please Enter Message In The Field',);
/*Behaviors*/
if ($_POST)
{
mail($to, $subject, $message, $header);
}
if(isset($_POST['message']))
{
echo $_POST['name/company'];
echo "<br />";
echo $_POST['email'];
echo "<br />";
echo $_POST['message'];
}
if(empty($_POST['name/company']))
{
echo "Name Field is Required";
}
if(empty($_POST['email']))
{
echo "Email Field is Required";
}
if(empty($_POST['message']))
{
echo "Message Field is Required";
}
$body = <<<EOD
<br><hr><br>
Name/Company: $name/company <br />
Email: $email <br />
Message: $message <br />
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail("$yourEmail", "$emailTitle", "$body", "$headers");
$sent = "Thank You, Your Inquiry Has Been Sent";
echo $result;
enter code here
}
?>

Mail is not coming properly using php?

I am using below simple code to send mail using php. when i echo the line $headers = "From: . $email\r\n"; //echo $headers; exit; which results like From: . test#email.com.
I found dot is printing before mail address , so mail is coming but not properly.If i remove dot , mail is not even coming.Whats wrong with this code ?
<?php
if (isset($_REQUEST['user_email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['user_email'];
$user_name = $_REQUEST['user_name'];
$user_message = $_REQUEST['user_message'];
include 'contact_mail_form.php';
$to = "testtest#gmail.com";
$message = $contactText;
$subject = "Test Mail";
$headers = "From: . $email\r\n"; //echo $headers; exit;
$headers .= "Content-type: text/html\r\n";
mail($to,$subject,$message,$headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
?>
<form id="contact_form" name="ContactForm" action="" method="post" onsubmit="return checkContact(this);">
<div class="wrapper">
<div class="fright">
<div class="name">Message:</div>
<textarea cols="1" rows="1" name="user_message" id="user_message" ></textarea>
<span class="clear"></span> </div>
<div class="fleft">
<div>
<div class="name">Your Name:</div>
<input type="text" class="input" name="user_name" id="user_name" />
<span class="clear"></span> </div>
<div>
<div class="name">E-mail:</div>
<input type="text" class="input" name="user_email" id="user_email" />
<span class="clear"></span> </div>
</div>
</div>
<div class="buttons"><a class="more" href="javaScript:void(0);" onClick="document.getElementById('contact_form').reset()">clear</a> <input class="more1" type="SUBMIT" class="button2" value="Send" name="" /> </div>
</form>
<?php } ?>
Try it like this your dot is acting as string not as concatenation
$headers = "From:" . $email."\r\n";
Maybe because \r\n should be surrounded with " not '
And Also
$headers .= 'Content-type: text/html'. "\r\n";

Categories