Can't find error PHP - php

So, this is my first attempt in PHP so it might be very easy to fix. I am trying to make a form which can be submitted and sent to administration's e-mail.
I am running a server with MAMP, going online filling the form, submitting it and I get error:
We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.
We are sorry, but there appears to be a problem with the form you submitted.
Please go back and fix these errors.
Full PHP code:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "I HAVE CORRECT EMAIL HERE 4 SURE";
$email_subject = "eShop Contact Form";
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['fname']) ||
!isset($_POST['lname']) ||
!isset($_POST['email']) ||
!isset($_POST['subject'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['fname']; // required
$last_name = $_POST['lname']; // required
$email_from = $_POST['email']; // required
//$country = $_POST['country']; // not required
$comments = $_POST['subject']; // 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(strlen($comments) < 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 .= "Country: ".clean_string($country)."\n";
$email_message .= "Subject: ".clean_string($comments)."\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
}
?>
This is the part that should be having problem:
// validation expected data exists
if(!isset($_POST['fname']) ||
!isset($_POST['lname']) ||
!isset($_POST['email']) ||
!isset($_POST['subject'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
P.S I have copied the code from http://www.freecontactform.com/email_form.php here and edited it a little.

Well, now I will start from here.
I will re-edit your code
SUBMIT.PHP
<?php
if(isset($_POST['formid'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "yourmail.com.id#maisetting.com";
$email_subject = "eShop Contact Form";
//
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mail = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comm'];
// I was replace this line
// validation expected data exists
if(empty($fname) or empty($lname) or empty($mail) or empty($subject) or empty($comments)){
$err_msg = 'We are sorry, but there appears to be a problem with the form you submitted.';
} else {
// check mail registered
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$mail)) {
$err_msg = 'The Email Address you entered does not appear to be valid.<br />';
} else {
// check name
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$fname)) {
$err_msg = 'The First Name you entered does not appear to be valid.<br />';
} else {
// check Last name
if(!preg_match($string_exp,$lname)) {
$err_msg = 'The Last Name you entered does not appear to be valid.<br />';
} else {
// Check comment
if($comments<2 or $comments=='') {
$err_msg = 'The Comments you entered do not appear to be valid.<br />';
} else {
// SEND msg from the visitor
$err_msg = 'Your Messages Was Send. ThankYou!';
$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($fname)."\n";
$email_message .= "Last Name: ".clean_string($lname)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
// $email_message .= "Country: ".clean_string($country)."\n";
$email_message .= "Subject: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
}}}}} } else { $err_msg = 'Add Your comment here'; }
?>
INDEX.PHP
<?php require('SUBMIT.PHP'); ?>
<form action="#" method="post" />
<p><label>First Name</label>
<input type="text" placeholder="First name here" name="fname" />
</p>
<p><label>Last Name</label>
<input type="text" placeholder="Last name here" name="lname" />
</p>
<p><label>Mail</label>
<input type="text" placeholder="Add yourmail here" name="mail" />
</p>
<p><label>Subject</label>
<input type="text" placeholder="Subject here" name="subject" />
</p>
<p><label>Your Comment</label>
<textarea name="comm"></textarea>
</p>
<p><input type="submit" name="formid" value="Send" /></p>
</form>
<?php echo $err_msg; ?>
Now, do and test.

Related

How to create a contact form and having it sent to your e-mail?

I'm currently working on a project; however, I'm in need of a contact form to have the user submit their information and have the information sent to an e-mail. I feel comfortable with HTML and CSS but not php. I'm trying to follow a tutorial online and it's quite difficult to follow. Could anyone please help?
Thanks!
Also, I would love it if it got sent to my thanks.html page. As in, when the user submits the form, it will redirect the user to my thanks.html page.
HTML:
<h1 id="contact-us">Let us know something!</h1>
<form action="contact.php" class="cf">
<div class="half left cf">
<input type="text" id="input-fname" placeholder="First Name">
<input type="text" id="input-lname" placeholder="Last Name">
<input type="email" id="input-email" placeholder="Email Address">
</div>
<div class="half right cf">
<textarea name="message" type="text" id="input-message" placeholder="Message"></textarea>
</div>
<input type="submit" value="Submit" id="input-submit">
</form>
php:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "asdf#myemail.com";
$email_subject = "my subject";
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['input-fname']) ||
!isset($_POST['input-lname']) ||
!isset($_POST['input-email']) ||
!isset($_POST['input-message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['input-fname']; // required
$last_name = $_POST['input-lname']; // required
$email_from = $_POST['input-email']; // required
$comments = $_POST['input-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,$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(strlen($comments) < 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 .= "Comments: ".clean_string($comments)."\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);
?>
<?php
}
?>
I've fixed it! Here is the code in case anyone is looking at this and wants to accomplish a contact form like mine.
Thanks!
HTML:
<h1 id="contact-us">Let us know something!</h1>
<form action="contact.php" method='post' class="cf">
<div class="half left cf">
<input type="text" id="fname" name="fname" placeholder="First Name">
<input type="text" id="lname" name="lname" placeholder="Last Name">
<input type="email" id="email" name="email" placeholder="Email Address">
</div>
<div class="half right cf">
<textarea name="message" type="text" id="message" placeholder="Message"></textarea>
</div>
<input type="submit" value="Submit" id="input-submit" name='send'>
</form>
php:
<?php
if(isset($_POST['send'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "hunter.shaw#cei.edu";
$email_subject = "D-Tail Doctor Form Info";
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['fname']) ||
!isset($_POST['lname']) ||
!isset($_POST['email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['fname']; // required
$last_name = $_POST['lname']; // required
$email_from = $_POST['email']; // 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,$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(strlen($comments) < 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 .= "Comments: ".clean_string($comments)."\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);
}
// redirects them to link below
header('Location: thanks.html');
?>

PHP Form not redirecting with Header- Won't redirect [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
Used this form template to create a form, and took out a few fields as they were not all necessary.
http://www.freecontactform.com/html_form.php
However, my header command is not redirecting my page. Here's the code. Please note that I have tried with and without exit at the end.
The problems isn't that it won't send out an email, but won't redirect.
<?php
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "emailt#yahoo.com";
$email_subject = "Email from email.com";
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['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // 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_comments .= '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(strlen($comments) < 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 .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\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);
header("Location: http://www.website.com/contactSoon.html");
}
?>
Here is the contact form I'm using-
<form name="htmlform" method="post" action="html_form_send.php">
<div class="row form-group">
<div class="col-md-12">
<label for="first_name">Name *</label>
<input type="text" name="first_name" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label for="comments">Email Address *</label>
<input type="text" id="email" class="form-control">
</div>
</div>
<div class="row form-group">
<div class="col-md-12">
<label for="message">Message *</label>
<textarea name="message" id="message" cols="30" rows="10" class="form-control"></textarea>
</div>
</div>
<div class="g-recaptcha" data-sitekey="6LfmXB4UAAAAAHxguDJIQXClAG_8rGe6qnK1SldM"></div>
<div class="form-group">
<input type="submit" name="submit" id="submit" value="Send Message" class="btn btn-primary">
</div>
</form>
Change to
<?php
//memory increase max
ini_set('memory_limit', '-1');
if(isset($_POST['email'])) {
// CHANGE THE TWO LINES BELOW
$email_to = "emailt#yahoo.com";
$email_subject = "Email from email.com";
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['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // 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_comments .= '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(strlen($comments) < 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 .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\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);
echo '<script>window.location.href = "http://www.website.com/contactSoon.html";</script>';
//header("Location: http://www.website.com/contactSoon.html");
}
?>
So it's going to redirect with javascript insted of php

php form submission is working but erroring instead of redirecting [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 8 years ago.
Can someone check this php code and tell me why I'm getting this error:
"Warning: Cannot modify header information - headers already sent by (output started at /home/itechcom/public_html/DesignsbyGabe.com/send_form_email.php:144) in /home/itechcom/public_html/DesignsbyGabe.com/send_form_email.php on line 146"
PHP:
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "Jonathansumner90#gmail.com";
$email_subject = "contact from Designs by Gabe form";
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['comments'])) {
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']; // not required
$comments = $_POST['comments']; // 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(strlen($comments) < 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 .= "Comments: ".clean_string($comments)."\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 -->
<?php
header("Location: thankyou.html");
?>
<?php
}
?>
HTML:
<form action="send_form_email.php" method="post" name="contactform">
<label for="first_name">First Name *</label>
<input type="text" name="first_name" size="30" maxlength="50" />
<label for="last_name">Last Name *</label>
<input type="text" name="last_name" size="30" maxlength="50" />
<label for="email">Email Address *</label>
<input type="text" name="email" size="30" maxlength="80" />
<label for="telephone">Telephone Number</label>
<input type="text" name="telephone" size="30" maxlength="30" />
<label for="comments">Comments *</label>
<textarea name="comments" rows="6" cols="25"></textarea>
<input id="submit" style="margin-right: 30px;" type="submit" value="Submit" />
This question has been asked before but not in this context. The messages go through but instead of redirecting it gives me an error. I use this same form submission code for other sites and it works fine.
Try this one :
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "Jonathansumner90#gmail.com";
$email_subject = "contact from Designs by Gabe form";
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['comments'])) {
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']; // not required
$comments = $_POST['comments']; // 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(strlen($comments) < 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 .= "Comments: ".clean_string($comments)."\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);
header("Location: thankyou.html");
}
?>
Put the header command inside the first PHP tags
#mail($email_to, $email_subject, $email_message, $headers);
header("Location: thankyou.html");
?>
You are doing it wrong. You can't set header after outputting something(not even a space). Just set headers on top like
<?php
header("Location: thankyou.html");
or use html meta redirect
<META http-equiv="refresh" content="0;URL=http://example.com/thankyou.html">
or use javascript
window.location.replace("http://example.com/thankyou.html");
Hope this helps you
It was an issue with The white space before the opening php tag surrounding the header. I have to assume it's the server I'm on being picky. I've heard of white space issues but i've never encountered one and I've used this form many times.
Thanks everyone for helping me out.
use exit after header('location:..'); when you redirecting to some page
like
header("Location: thankyou.html");
exit;
and also *Not To print any think before it*
such as you are using comment of html in php like
<!-- include your own success html here -->
remove above line or make it php comment as
// include your own success html here

php contact form submit just takes me to action URL

I have added a simple contact form to my site, however when I click submit, I am taken to the url of the action attribute. Below is the html for my form:
<form name="quickcontact" method="post" action="includes/quick_contact.php" id="quickcontact">
<input type="text" name="first_name" maxlength="50" size="30" placeholder="Name">
<input type="text" name="telephone" maxlength="30" size="30" placeholder="Number">
<input type="submit" value="Call Me" id="submit">
</form>
And the php file that is referenced:
<?php
if(isset($_POST['email'])) {
$email_to = "sam.skirrow#gmail.com";
$email_subject = "AAO - Someone wants you to call them back";
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['telephone'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$telephone = $_POST['telephone']; // 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(strlen($comments) < 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 .= "Telephone: ".clean_string($telephone)."\n";
$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)) {
header('location: ../success.php');
} else {
header('location: ../fail.php');
}
}
?>
can anyone see why this might be happening??
Your issue is with this line:
if(isset($_POST['email'])) {
Your form has no input with the name email therefore this test will never be true and the entire script is inside this condition. You are submitting $_POST['first_name'] or $_POST['telephone'] you should check if one of those two isset.

email not send in php why

Hi i have created a page contact us but it's not work why where i m wrong
Pls Help me
contact page code
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "azadrohit#gmail.com";
$email_subject = "Your email subject line";
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['user_name']) ||
!isset($_POST['contact_no']) ||
!isset($_POST['contact_email_id']) ||
!isset($_POST['city']) ||
!isset($_POST['project']) ||
!isset($_POST['local_property']) ||
!isset($_POST['user_query'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$user_name = $_POST['user_name']; // required
$contact_no = $_POST['contact_no']; // required
$email_from = $_POST['contact_email_id']; // required
$city = $_POST['city']; // not required
$project = $_POST['project']; // not required
$local_property = $_POST['local_property']; // not required
$user_query = $_POST['user_query']; // 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,$user_name)) {
$error_message .= 'The Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$contact_no)) {
$error_message .= 'The Contact Number you entered does not appear to be valid.<br />';
}
if(strlen($user_query) < 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 .= "User Name: ".clean_string($user_name)."\n";
$email_message .= "Contact No: ".clean_string($contact_no)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "City: ".clean_string($city)."\n";
$email_message .= "Project: ".clean_string($project)."\n";
$email_message .= "Local Property: ".clean_string($local_property)."\n";
$email_message .= "User Query: ".clean_string($user_query)."\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
}
?>
html code
<div class="contact_form">
<p>Quick Contact</p>
<form action="contactus.php" method="post" name="contactform">
<label><span>name:-</span> <input type="text" name="user_name"></label>
<label><span>contact no:-</span> <input type="text" name="contact_no"></label>
<label><span>email id:-</span> <input type="text" name="contact_email_id"></label>
<label><span>city:-</span> <input type="text" name="city"></label>
<label><span>projects:-</span> <input type="text" name="project"></label>
<label><span>local property:-</span> <input type="text" name="local_property"></label>
<label><span>query:-</span> <textarea row="" cols="" name="user_query"></textarea></label>
<input type="submit" value="submit">
</form>
</div>
add this to your code ...
$headers = "From: \"$from_name\" <$from_mail>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/plain; charset=\"utf-8\"\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";

Categories