Addressing visitor by name after contact form submision - php

I have the lines of code below on my "contact page" and I'd like to thank the address the visitor by their name when when they're redirected to "thank you page" But the "thank you"page shows up with just "Thanks for contacting us.
<?
$mailto = 'info#siteripe.com'; // insert the email address you want the form sent to
$returnpage = 'thanks.php'; // insert the name of the page/location you want the user to be returned to
$sitename = '[siteripe.com]'; // insert the site name here, it will appear in the subject of your email
$name = $_POST['name'];
$email = $_POST['email'] ;
$subject = $_POST['subject'];
$message = $_POST['message'];
$phone = $_POST['phone'];
if (!$name) {
print("<strong>Error:</strong> Please provide your name.<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
exit;
}
if (!$email) {
print("<strong>Error:</strong> Please provide an email address.<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
exit;
}
if (!$subject) {
print("<strong>Error:</strong> Please provide a subject.<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
exit;
}
if (!$phone) {
print("<strong>Error:</strong> Please provide a Phone number<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
exit;
}
if (!$message) {
print("<strong>Error:</strong> Please provide a Message<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
exit;
}
if (!eregi("^[a-z0-9]+([-_\.]?[a-z0-9])+#[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}", $email)){
print("<strong>Error:</strong> this email address is not in a valid format.<br/><br/><a href='javascript:history.go(-1)'>Back</a>");
exit;
}
$message = "\n$name submitted the following message:\n\n$message\n\nSender's contact details are as follows:\n\nName: $name\nPhone Number: $phone\nEmail Address: $email\n";
mail($mailto, "$subject", $message, "From: $email");
header("Location: " . $returnpage);
?>
On the Thank you page I have the code below
<?php echo $_POST["name"]; ?> Thanks for contacting us<br />
Thanks

The reason that the name is not showing up is that you are not passing any data to it. You can update your send php file using the codes below:
header('Location:http://www.domain.com/thankyou.php?name='.$name);
thankyou.php
<?php echo $_GET['name']; ?> Thanks for contacting us

$_POST lives only for the current request, so if you change page the array will be gone.You could use $_SESSIONS instead to store more persistent datas.
Be careful of having session_start() called at the top of your page when using $_SESSIONS.
$_SESSION['name'] = $_POST['name'];
On your thankyou page:
<?php echo isset($_SESSION['name']) ? $_SESSION['name'] : '';?> Thanks for contacting us<br />

Related

Multiple Email Addresses for Form Submission

I have two scenarios for my form that will determine if the submission is successful or failed. Either outcome serves the user with a unique page. I am looking for a way that the successful submissions are emailed to group A email addresses and the failed submissions are sent to Group B email addresses.
Here is my code:
<?php session_start();
if(isset($_POST['Submit'])) {
$from=$_POST['empty'];
$to=$_POST['empty'];
$subject=$_POST['empty'];
$text=$_POST['empty'];
$html=$_POST['empty'];
$file=$_POST['empty'];
$mimetype=$_POST['empty'];
$host=$_POST['empty'];
$username=$_POST['empty'];
$password=$_POST['empty'];
$Q1=$_POST['Q1'];
$Q2=$_POST['Q2'];
$Q3=$_POST['Q3'];
$Q4=$_POST['Q4'];
$fromsubject = 'sample-subject';
$firstname = $_POST['firstname'];
$mail = $_POST['mail'];
$phonenumber = $_POST['phonenumber'];
$to = 'myemail#gmail.com';
$header = "From: <" . $mail . ">\r\n"; //optional headerfields
$mailsubject = 'Message recived from sample site';
$body = $fromsubject.'
The person that contacted you is '.$firstname.'
Phone number: '.$phonenumber.'
E-mail: '.$mail.'
Q1: '.$Q1.'
Q2: '.$Q2.'
Q3: '.$Q3.'
Q4: '.$Q4.'
|---------END MESSAGE----------|';
mail($to, $mailsubject, $body, $header);
if ($Q3 == "yes") {
header('Location: http://example.com/success.php');
} else {
header('Location: http://example.com/resources.php');
}
?>
I apologize for the confusion. My form has 4 questions in it. If they answer no to one of them, they do not qualify for our offer. If they answer yes to all, they qualify. The all yes users need to be sent to our call center email address while the other respondents who did not answer all yes go to our recovery department. –

PHP form to returning all the information

I am a complete novice when it comes to php, I have got the below php which sends me back the 'message' and sends an auto response to the user, as well as redirecting them to the 'thank you' page. Problem I am having is that it won't return the users name that they fill in on the form, any ideas?
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.', $message, "From: Krew Kut Hair<$email>")) {
$autoreply = "Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Assuming the name is in one of the form fields, you should be able to retrieve it. As Barmar says - all you have to do is use it somewhere in the body or the message. How can you tell the name is missing if you don't echo it out somewhere.
Try this:
$autoreply = "Thank you ".$name." for ...
If the name is still "missing" - you can try to see all the post variables like this:
echo "<PRE>Post Vars\n"; print_r($_POST);
If you have an input named "name" like:
<input type="text" name="name" value="" />
Check if it's containing data with e.g. :
echo 'The value of name is ['.$name.']';
If it is containing data you just can use the $name variable in your message. If it isn't there is probably something wrong in your HTML form.
<?php
$youremail = "ally.baird81#gmail.com"; //this is where the email will be sent to
#extract($_POST);$name = filter_var($name, FILTER_SANITIZE_STRING);
$message = filter_var($message, FILTER_SANITIZE_STRING);
$content = "<strong>Name:<strong><br />".$name."<br />";
$content .= "<strong>Message:<strong><br />".$message."<br />";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (mail($youremail, 'Message from website.<br />', $content, "From: Krew Kut Hair<$email>")) {
$autoreply = "Hi ".$name.". Thank you for enquiring at Krew Kut Hair, we will be in contact shortly";
$subject = "Thank you for your enquiry!";
mail($email, $subject, $autoreply, "From: Krew Kut Hair<$email>");
}
} else {
echo "Please enter a valid email address";
}
header("Location: thanks.html");
Also read the comments on your question. I strongly recommend to find an other way instead of using extract().

PHP reply-to error - need person's email, not admin

I have the below PHP contact form that has a CAPTCHA code to ensure is correct. However, when I reply to the email from the website it puts a random email which i believe is the server admin, however, I want it to be the persons email who sent the form in. below is the code, could you possibly be able to help me?
<?php session_start();
if(isset($_POST['Submit'])) { if( $_SESSION['chapcha_code'] == $_POST['chapcha_code'] && !empty ($_SESSION['chapcha_code'] ) ) {
$youremail = 'info#example.com';
$fromsubject = 'www.example.co.uk';
$title = $_POST['title'];
$fname = $_POST['fname'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$to = $youremail;
$mailsubject = 'Message from Website'.$fromsubject.' Contact Page';
$body = $fromsubject.'
The person that contacted you is: '.$fname.'
Phone Number: '.$phone.'
E-mail: '.$mail.'
Subject: '.$subject.'
Message:
'.$message.'
|---------END MESSAGE----------|';
echo "Thank you for your message. I will contact you shortly if needed.<br/>Go to <a href='/index.html'>Home Page</a>";
mail($to, $subject, $body);
unset($_SESSION['chapcha_code']);
} else {
echo 'Sorry, you have provided an invalid security code';
}
} else {
echo "You must write a message. </br> Please go to <a href='/contact.html'>Contact Page</a>";
}
?>
You'll need some headers so the from address is the users mail.
Also refer to the mail docs
try this
$headers = "From: $mail\r\n";
$headers .= "Reply-To: $mail\r\n";
mail($to, $subject,$body,$headers);

displaying name on next page once email is sent

I have a form which sends to my email. Once form is complete and submitted it redirects to a email sent conformation page. I want to display the name on this page grabbing it from the field in which they inserted their name.
EMAIL FORM PHP:
<?php
$your_email ='info#example.com';
session_start();
$errors = '';
$name = '';
$visitor_email = '';
$user_message = '';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$user_message = $_POST['message'];
///------------Do Validations-------------
}
?>
header('Location: emailsent.php?name=' . $name);
In emailsent.php
echo $_GET['name'];
May I ask why are you using header('Location: emailsent.php');? instead of using header you can reload the same page and modify it to show a different content if the email was sent.
<?php
if(isset($_POST['submit']) && empty($errors)) {
//show a success message and some html code... e.g.:
echo 'Thank you, '.$_POST['name']; //this is the senders name
}
?>
If you must use headers, here is a solution

php contact form with reply message

I created a contact form that uses my php script to email the form parameters:
//File email.php
<?php
include('variables.php');
$to = "info#timeshare.org.uk";
$subject = "Quote From Website";
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$resort = $_POST["resort"];
$headers = "From: ".$to."\r\n";
$message = "Name: ".$name."\r\n".
"Email: ".$email."\r\n".
"Number: ".$number."\r\n".
"Resort Name:".$resort."\r\n";
if (mail($to,$subject,$message,$headers)){
$replyHeader = "Thank You!";
$replyMessage = "Thank you for using our quick contact form. We will get back to you as soon as possible.";
}else{
$replyHeader = "Oops!";
$replyMessage = "An Error Occured whilst trying to send the form. Please try again later";
}
header( 'Location: http://localhost/TimeShare/formReturn.php' ) ;
?>
on submit the form action points to this code.
Now if the mail function is successfull, i want the form to redirect to formReturn.php, however i want the content in a particular div to show the $replyHeader and $replyMessage.
So once the form is posted, it redirects to a page that either displays a successfull message or an error message.
Now on my formReturn.php page ive tried including the variables Like so:
//File: formReturn.php
<body>
//Header code...
//sidebar code...
<div>
<?php include('php/email.php'); ?>
<h1> <?php echo $replyHeader; ?> </h1>
<p> <?php echo $replyMessage ?> </p>
</div>
//Footer code...
</body>
Problem with this code is, because im including the email.php file, i end up with a redirection loop. Which is bad!
So how would i get the variables $replyHeader and $replyMessage onto the page in that specific location depending on the success or failure of the mail() function
You can also add a GET-variable to your second page:
if($mail_is_succesfull)
header('Location: http://localhost/TimeShare/formReturn.php?success=true');
else
header('Location: http://localhost/TimeShare/formReturn.php?success=false') ;
Then you can add the message in your other page.
you could pass it as a get variable
<?php
include('variables.php');
$to = "info#timeshare.org.uk";
$subject = "Quote From Website";
$name = $_POST["name"];
$email = $_POST["email"];
$number = $_POST["number"];
$resort = $_POST["resort"];
$headers = "From: ".$to."\r\n";
$message = "Name: " . $name . "\r\n".
"Email: " . $email . "\r\n".
"Number: " . $number . "\r\n".
"Resort Name:". $resort . "\r\n";
if (mail($to,$subject,$message,$headers)){
$replyHeader = "Thank You!";
$replyMessage = "Thank you for using our quick contact form. We will get back to you as soon as possible.";
header( "Location: http://localhost/TimeShare/formReturn.php?header={$replyHeader}&message={$replyMessage}" ) ;
}else{
$replyHeader = "Oops!";
$replyMessage = "An Error Occured whilst trying to send the form. Please try again later";
header( "Location: http://localhost/TimeShare/formReturn.php?header={$replyHeader}&message={$replyMessage}" ) ;
}
?>
//File: formReturn.php
<body>
<div>
<h1> <?php echo $_GET['header'] ?> </h1>
<p> <?php echo $_GET['message'] ?> </p>
</div>
</body>
just update your email thing to send email and redirect ONLY IF
if (isset($_POST["email"])){
}
otherwise you know that form is not submitted and that you don't want to bother about sending email.
and as others suggested, you have to append some get param to the url, like Snicksie (header('Location: http://localhost/TimeShare/formReturn.php?success=[1|0]');)
suggested. then on your script you check for that variable to see if you should show anything.
e.g.
if (isset($_GET["success"])){
if ($_GET["success"] == 1){
show success message
} else {
show error message
}
}

Categories