PHP Validation to require One of Two Form Fields filled in - php

I am developing a site that uses javascript to handle many functions and PHP based Captcha code to validate the form field.
It works great, but the form will submit if none of the fields are filled in.
I need one of two form fields ['email' or 'phone'] filled in, but neither can be left blank.
The error message can be the same error message thrown up when the captcha field is left blank or filled in incorrectly.
I am new to PHP code and cannot figure out for the life of me how to call the function.
The function code is:
<?php
if(isset($_POST['send'])){
$emailFrom = "clientemail.com";
$emailTo = "clientemail.com";
$subject = "Contact Form Submission";
$first_name = strip_tags($_POST['first_name']);
$last_name = strip_tags($_POST['last_name']);
$email = strip_tags($_POST['email']);
$phone = strip_tags($_POST['phone']);
$message = strip_tags(stripslashes($_POST['message']));
$body .= "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
$headers = "From: ".$emailFrom."\n";
$headers .= "Reply-To:".$email."\n";
if($_SESSION['security_code'] == $_POST['security_code']){
$success = mail($emailTo, $subject, $body, $headers);
if ($success){
echo '<p class="yay">Your e–mail has been sent.</p>';
}
} else {
echo '<p class="oops">Something went wrong. Hit the back button and try again.</p>';
}
} else {
?>
The form field:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post" id="contact" name="contact">
<fieldset>
<label for="name"><span style="color:#bf252b;">*</span>First Name</label>
<input type="text" id="first_name" name="first_name" minlength="2"/>
<label for="name">Last Name</label>
<input type="text" id="last_name" name="last_name" minlength="2"/>
<label for="email"><span style="color:#bf252b;">*</span> Email</label>
<input type="text" id="email" name="email" />
<label for="phone"><span style="color:#bf252b;">*</span> Phone</label>
<input type="text" id="phone" name="phone" />
<label for="message">Message</label><div style="clear:both;"></div>
<textarea id="message" name="message" cols="40" rows="10" ></textarea>
<img src="../captcha.php" id="captcha" alt="captcha" style="padding:25px 0px 20px 0px;" />
<label for="security_code">Enter captcha</label>
<input type="text" id="security_code" name="security_code" autocomplete="off" class="required"/>
<button type="submit" id="send" name="send" style="margin:0px 0px 10px 12px;">Send!</button>
</fieldset>
</form>
<?php } ?>
There is a .php document for running captcha, but am I right in thinking there is a simple solution for this; some extra code in the existing code that will fix my issue? I really want to avoid javascript and plugins if I can help it.
Thanks in advance!!

Try this,
if($_SESSION['security_code'] == $_POST['security_code'] && (!empty($email) || !empty($phone))) {
instead of
if ($_SESSION['security_code'] == $_POST['security_code']) {
This is not convenient way to validate form but I hope this will help.

Add the required attribute to the fields you need required.

Fix your code!
Notice: Undefined variable: body in /../sendform.php on line 14
Do that:
$body = "First Name: ".$first_name."\n";
$body .= "Last Name: ".$last_name."\n";
$body .= "Email: ".$email."\n";
$body .= "Phone: ".$phone."\n";
$body .= "Comments: ".$message."\n";
Solution
if(($_SESSION['security_code'] == $_POST['security_code']) AND
(!empty($email) OR !empty($phone)) ) {
In other words:
Security code must be match
Email or Phone can not be empty

Related

Passing variables after form submit PHP [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 2 years ago.
Is there an efficient way to pass variables from one page to another after form submission? I'm struggling to maintain the variables that are submitting on the form page to display them on the confirmation page after being redirected on submission.
Could it be the way i'm 'storing' the data with $_POST? Should I be using sessions? If I should how would I go about storing the $_POST in $_SESSION and being able to call it in the email template as a $variable-name format? Is using header(); to redirect inefficient in this manner and maybe redirecting via ajax? Not sure how I would approach that if so.
Form:
<form id="pricing-form-inquiry" action="<?php echo get_stylesheet_directory_uri(); ?>/pricing-form/form-handler.php" method="POST" role="form">
<div class="pricing-modal" id="modal1" data-animation="slideInOutLeft">
<div class="modal-dial">
<header class="modal-head">
<a class="close-modal" aria-label="close modal" data-close></a>
</header>
<section class="modal-body">
<div class="row">
<div class="col">
<input type="text" class="" placeholder="First Name" name="first-name" required data-error="First Name is required.">
</div>
<div class="col">
<input type="text" class="" placeholder="Last Name" name="last-name" required data-error="Last Name is required.">
</div>
</div>
<input type="email" class="" placeholder="Email Address" name="email" required data-error="Email Address is required.">
<input type="text" class="" placeholder="Company" name="company" id="company">
<input type="text" class="" placeholder="Phone Number" name="phone" id="phone">
<div class="row">
<div class="col text-center"></div>
</div>
</section>
<footer class="modal-foot">
<input type="submit" class="pricing-form-submit" value="Calculate" name="submit">
</footer>
</div>
</div>
</form>
form-handler.php
if(isset($_POST['submit'])) {
$first_name = filter_var($_POST['first-name'], FILTER_SANITIZE_STRING);
$last_name = filter_var($_POST['last-name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$company = filter_var($_POST['company'], FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$to = "email#email.com"; // Email Address to send lead to
$subject = "Subject Goes Here!"; // Subject of generated lead email
// HTML Message Starts here
$message = "<html><body><table style='width:600px;'><tbody>";
$message = "<tr><td style='width:150px'><strong>Name: </strong></td><td style='width:400px'>$first_name $last_name </td></tr>";
$message = "<tr><td style='width:150px'><strong>Email Address: </strong></td><td style='width:400px'>$email</td></tr>";
$message = "<tr><td style='width:150px'><strong>Company Name: </strong></td><td style='width:400px'>$company</td></tr>";
$message = "<tr><td style='width:150px'><strong>Phone Number: </strong></td><td style='width:400px'>$phone</td></tr>";
$message = "</tbody></table></body></html>";
// HTML Message Ends here
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: Company <from#email.com>' . "\r\n"; // User will get an email from this email address
// $headers .= 'Cc: from#email.com' . "\r\n"; // If you want add cc
if(mail($to,$subject,$message,$headers)){
// Message if mail has been sent
echo "<script>
alert('Mail has been sent Successfully.');
</script>";
header("Location: /pricing-confirm/");
} else {
// Message if mail has been not sent
echo "<script>
alert('EMAIL FAILED');
</script>";
}
}
To pass your variables onto the pricing-confirm page, you could pass the variables in your header() function like so
header("Location: /pricing-confirm.php?name=" . $first_name);
Once on your pricing-confirm.php page, you can grab the variables from the query string
if(isset($_GET['name']) {
$name = $_GET['name'];
}
If you want to pass multiple variables at once, you can either use & in the query string, or use urldecode with an array like this
$arr = [
"firstname" => $first_name,
"lasttname" => $last_name,
]
header("Location: /pricing-confirm.php?userdetails=" . urlencode(serialize($arr)));
If you have used serialize, you can get the values in the array like this
$queryArr = unserialize(urldecode($_GET['userdetails']));
you can then access them with their array key, like so
if(isset($_GET['userdetails']) {
$arr = $_GET['userdetails'];
if(isset($arr['firstname']) {
$firstName = $arr['firstname'];
}
}

Php mail function sends email every time page is refreshed

I used the mail function to do a contact form and it all works fine. However, I just noticed that after sending an email, every time a refresh the page, even though the fields are empty, an email keeps being sent.
My code looks like this:
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option>Select Current Situation</option>
<option class="placeholder" value="Unemployed">Unemployed</option>
<option class="placeholder" value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
echo $result;
}
}
}
?>
</form>
How can I make the site forget all the details from the input fields once an email is sent?
I tried to follow this question here but I don't seem to be able to make it work on my site
The easiest way is to add an forwarding in your code, like that:
EDIT: at #CD001 commentary
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
// header('Location: ?successfull-submit'); exit; // this one would fail because above is an output.
echo '<meta http-equiv="refresh" content="0; url=?successfull-submit">'; // its not a good /nice alternative but it "works".
Redirect to ?sent=1 without sending any output. And check 'sent' to determine whether or not to display the success message. Try below (assuming your script is contact.php). Also make sure
contact.php
<?php
$result = '';
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
//$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
//echo $result;
header('Location:' . 'contact.php?sent=1');
exit;
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
//echo $result;
}
}
}
if(isset($_GET['sent'])) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
}
echo $result;
?>
<form role="form" method="POST">
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option>Select Current Situation</option>
<option class="placeholder" value="Unemployed">Unemployed</option>
<option class="placeholder" value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
try like this,in your success message part..
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
echo "<script>alert('Mail sent Successfully');</script>";
echo "<script>window.location = 'contact.php';</script>";
} else {
echo "<script>alert('Mail not sent');</script>";
echo "<script>window.location = 'contact.php';</script>";
}
}
}
?>
while redirect to another page you can restrict that duplication problem....
Just try redirecting to another page or another function that discards the old $_POST data.
You could use session variable and reset it after each request.
Or, prevent reloading same page using javascript.
Or, redirect to some other page upon completion (if possible)
You can try to add CSRF token to your page to prevent double submission.
Refer to this link: How to prevent multiple form submission on multiple clicks in PHP
When the user refreshes the page, it is possible that the same parameters are getting posted again. As a result, if (isset($_POST["submit"])) This condition becomes true and mail will be sent every time user reloads.
One solution is to redirect to the same page or to a different page on success full completion.
ie,
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
}
Instead of the above method, redirect user to the same page or different page and show a message there. If you want to show the same page you can redirect with a flag in the query string as ?show_success_msg= true.
Then do this.
if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] == 'true') {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
}
Complete solution here:
<?php
// Handle PHP code always on Top of the page (ie, Not after your HTML), You cant send headers if you have any content above it.
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile'];
$subject_line = $_POST['subject'];
$situation = $_POST['situation'];
$from = 'myemail#email.co.za';
$to = 'myemail#email.co.za';
$subject = 'SchoemanLaw lead ...';
$body ="From: $name <br/> E-Mail: $email <br/> Mobile: $mobile Subject: $subject_line <br/> Situation: $situation";
//$body ="From: $name\r\n E-Mail: $email\r\n Mobile:\r\n $mobile Subject: $subject_line\r\n Situation:\r\n $situation";
// set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers optional/headers
$headers .= "From:$from";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
// Check if mobile has been entered
if (!$_POST['mobile']) {
$errMobile = 'Please enter your number';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMobile) {
if (mail($to,$subject,$body,$headers)) {
$result='<div class="alert alert-success">Thank You ! We will be in touch soon</div>';
echo $result;
} else {
header("Location: your_page.php?show_success_msg=true")
}
}
}
?>
<form role="form" method="POST">
<?php if(isset($_GET['show_success_msg']) && $_GET['show_success_msg'] ==
'true') {
$result='<div class="alert alert-success">Thank You ! We will be in touch
soon</div>';
echo $result;
} ?>
<br style="clear:both">
<h3 style="margin-bottom: 25px; text-align: center;">Contact a Conveyancing Property Lawyer Now</h3>
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="email" name="email" placeholder="Email" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="mobile" name="mobile" placeholder="Contact Number" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Subject" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> required>
</div>
<div class="form-group">
<select name="situation" id="situation">
<option>Select Current Situation</option>
<option class="placeholder" value="Unemployed">Unemployed</option>
<option class="placeholder" value="Employed">Employed</option>
</select>
</div>
<button type="submit" id="submit" name="submit" class="btn btn-primary">Submit</button>

How to apply Google reCAPTCHA (2.0) to my current HTML/PHP contact form

I am trying to apply Google reCAPTCHA to my HTML/PHP contact form. The reCAPTCHA appears on the page and works, but I cannot get it to work with the form.
Can somebody please tell me how to add the appropiate PHP code to integrate the recaptcha into my contact form?
<form action="submit.php" method="POST">
<input class="textbox" type="text" name="fullname" placeholder="Your Name">
<input class="textbox" type="email" name="email" placeholder="E-Mail">
<input class="textbox" type="tel" name="telephone" placeholder="Telephone">
<input class="textbox" type="text" name="business" placeholder="Organization">
<textarea class="comments_box" name="visitor_message" placeholder="Message"></textarea>
<div class="g-recaptcha" data-sitekey="6LddnxAUAAAAAAi6jmmUcX8pPMfSELRgpNUAI2Ra"></div>
<br>
<input class="submitBtn" type="submit" value="SUBMIT">
</form>
<?php
if (isset($_POST['fullname']) && isset($_POST['email']) && isset($_POST['telephone']) && isset($_POST['business']) && isset($_POST['visitor_message'])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$telephone= $_POST['telephone'];
$business = $_POST['business'];
$visitor_message = $_POST['visitor_message'];
if (!empty($fullname) && !empty($email) && !empty($telephone) && !empty($business) && !empty($visitor_message)) {
$to = 'acramirez38#gmail.com';
$subject = 'Trucker Radio Talk Visitor Message';
$body = "Full Name: " . $fullname ."\n". "E-Mail: " . $email ."\n". "Telephone: " . $telephone ."\n". "Business: " . $business ."\n". "Comments: " . $visitor_message;
$headers = 'From: ' .$email;
if (mail($to, $subject, $body, $headers))
echo 'Thank you for contacting us! Your message has been successfully delivered.';
} else {
echo 'All fields are required! Please return to the previous page and revise.';
}
}
?>

i am not getting confirmation mail when a user fill my form

I have a website. i want to get form details from my customer...
i am preparing some code. The code works good on some hosting panel but not working in my hosting panel.
i think some functions are not supporting my hosting.
i report the hosting provider but there is no good solution.
can any one solve this...
Thanks in advance
my advertisement.html form is looking like this
<form action="advertisement-form.php" method="post" class="comment-form row-fluid" data-validate="parsley">
<p>
<label for="name" class="span2">Your Name</label>
<input type="text" class="span10" placeholder="Your Name" id="name" name="name" required >
</p>
<p>
<label for="email" class="span2">E-mail</label>
<input type="email" class="span10" placeholder="Your E-mail" id="email" name="email" required >
</p>
<p>
<label for="name" class="span2">Your Ad Title</label>
<input type="text" class="span10" placeholder="Ad Title" id="ad_title" name="ad_title" required >
</p>
<p>
<label for="site" class="span2">Target URL</label>
<input type="text" name="target_url" id="target_url" class="span10" data-type="url" required>
</p>
<p>
<label for="site" class="span2">Banner URL</label>
<input type="text" name="banner_url" id="banner_url" class="span10" data-type="url" required>
</p>
<p>
<label for="site" class="span2">Payment Email</label>
<input type="text" name="payment_email" id="payment_email" class="span10" data-type="url" required>
</p>
<p>
<label for="mess" class="span2">Message</label>
<textarea name="message" class="span10" data-trigger="keyup" data-rangelength="[20,1000]"></textarea>
</p>
<p>
<input type="submit" value="Continue" class="button button-load large-button offset2 span4">
</p>
</form>
and my form processing is looking like this, named with advertisement-form.php
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "myemail#gmail.com";
$email_subject = "New Advertisement for Website";
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['email']) ||
!isset($_POST['ad_title']) ||
!isset($_POST['target_url']) ||
!isset($_POST['banner_url']) ||
!isset($_POST['payment_email']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$ad_title = $_POST['ad_title']; // required
$target_url = $_POST['target_url']; // required
$banner_url = $_POST['banner_url']; // required
$payment_email = $_POST['payment_email']; // not required
$message = $_POST['message']; // 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: ".clean_string($email_from)."\n";
$email_message .= "Ad Title: ".clean_string($ad_title)."\n";
$email_message .= "Target URL: ".clean_string($target_url)."\n";
$email_message .= "Banner URL: ".clean_string($banner_url)."\n";
$email_message .= "Payment Email: ".clean_string($payment_email)."\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);
?>
when a user fill the form he got success message. but i am not receiving confirmation mail. this script working some hosting panels, not in my hosting panel.can any one solve this...
Thank you once again.
1 - change
died('We are sorry, but there appears to be a problem with the form you submitted.');
to
die('We are sorry, but there appears to be a problem with the form you submitted.');
2- remove # before mail function. As using # before any statement will suppress errors if anything goes wrong. So if you remove this.. It would be easy to troubleshoot.
Follow the 2 steps above and try again. If you see any error, paste them here
I will suggest this as a try, ending each line with \r\n and also adding a To header (redundant as it might seem). Also, declaring charset=utf-8 in the header should be enough.
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
// Additional headers
// This might look redundant but some services REALLY favor it being there.
$headers .= "To: $to_fullname <$to_email>\r\n";
$headers .= "From: $from_fullname <$from_email>\r\n";
Also the function is die not died

HTML/PHP Contact Form wont send to my email but goes to success page

Hello I'm new to stackoverflow so forgive me if I'm doing anything wrong. Also I have very little knowledge on PHP. So I have this contact form that I want to send to my email when the user submits it. I have been beating my head against the wall for a while now, both files are uploaded to the server, and are in the same directory. The form goes to the success page, but I get no email. help me please.
Also the link to the form is http://xtcracingteam.com/apply.html if that helps.
This is the HTML
<form method="post" action="http://xtcracingteam.com/send_form_email.php">
<label class="first-name">
First Name
</label>
<input name="firstName" class="firstName" type="text" placeholder="Joe" required />
<label class="last-name">
Last Name
</label>
<input name="lastName" class="lastName" type="text" placeholder="Swanson" required/>
<label class="Race-number">
Race Number
</label>
<input name="raceNumber" class="raceNumber" type="text" placeholder="999" required/>
<label class="class">
Class
</label>
<input name="bikeClass" class="bikeClass" type="text" placeholder="Mx 450 Production A" required/>
<label class="age">
Age
</label>
<input name="personAge" class="personAge" type="text" placeholder="21" required/>
<label class="home-town">
Home Town
</label>
<input name="homeTown" class="homeTown" type="text" placeholder="Washougal Washington" required/>
<label class="Current-sponsors">
Current Sponsors
</label>
<textarea name="sponsors" placeholder="Gopro, Redbull, Scott..." class="currentSponsors" required></textarea>
<label class="recent-achievements">
Recent Achievements
</label>
<textarea name="recentAchievments" placeholder="I won this and that..." class="recentAchievments" required></textarea>
<label class="why">
Why should we pick you to represent our brand?
</label>
<textarea name="theWhy" placeholder="(hint: Dont say Cuz im the fastest)" class="theWhy" required></textarea>
<label class="email">
Email
</label>
<input name="email" class="email" type="email" placeholder="Example#emails.com" required/>
<input class="submit" type="submit" value="Submit!" />
</form>
This is the PHP code
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "xtc.racing.team#gmail.com";
$email_subject = "New Application";
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['firstName']) ||
!isset($_POST['lastName']) ||
!isset($_POST['raceNumber']) ||
!isset($_POST['bikeClass']) ||
!isset($_POST['personAge']) ||
!isset($_POST['homeTown']) ||
!isset($_POST['sponsors']) ||
!isset($_POST['recentAchievments']) ||
!isset($_POST['theWhy']) ||
!isset($_POST['email'])) {
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
$raceNumber = $_POST['raceNumber']; // required
$bikeClass = $_POST['bikeClass']; // required
$personAge = $_POST['personAge']; // required
$homeTown = $_POST['homeTown']; // required
$sponsors = $_POST['sponsors']; // required
$recentAchievments = $_POST['recentAchievments']; // required
$theWhy = $_POST['theWhy']; // 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 .= "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 .= "race number: ".clean_string($raceNumber)."\n";
$email_message .= "bikeClass: ".clean_string($bikeClass)."\n";
$email_message .= "personAge: ".clean_string($personAge)."\n";
$email_message .= "homeTown: ".clean_string($homeTown)."\n";
$email_message .= "sponsors: ".clean_string($sponsors)."\n";
$email_message .= "recentAchievments: ".clean_string($recentAchievments)."\n";
$email_message .= "theWhy: ".clean_string($theWhy)."\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.
return to XTCRacingTeam
<?php
}
?>

Categories