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
Related
It saying i have a mistake and I've done everything right i hope. I'm using localhost for now.. may that be the problem? if not what is.
I get an error on this row
$name = $_POST['name'];
code:
<?php
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
?>
<form action="submit.php" method="post" name="contact_form">
<input name="name" id="name" type="text">
<input type="submit" name="submit" id="sumitbtn" value="submit">
</form>
There could be a number of factors as to why you are having difficulties with your mail form.
Check to see if mail() is installed and properly configured on your system.
Use additional headers(); one being a From: which is missing in your code, although not mandatory.
Mail will still attempt to send, however it could end up being sent to SPAM or ignored.
Use conditional statements for your submit button and mail() success, including checking if the field is left empty or not.
The following has been successfully tested, sent and received in my INBOX.
PHP
<?php
if (isset($_POST['submit']) && !empty($_POST['name'])){
$name = $_POST['name'];
$to = "email#example.com";
$subject = "add this";
$headers="From: $name <email#example.com>";
$sent = mail($to, $subject, $name, $headers);
if($sent) {
echo "Success.";
} else {
echo 'Sorry, your message could not be sent.';
}
} // brace for submit conditional statement
?>
You can also use the following:
<?php
if (isset($_POST['submit']) && !empty($_POST['name'])){
$name = $_POST['name'];
$from = "user#example.com";
$to = "your#example.com";
$subject = "add this";
$headers="From: $name <$from>";
$sent = mail($to, $subject, $name, $headers);
if($sent) {
echo "Success";
} else {
echo 'Sorry, your message could not be sent.';
}
} // brace for submit conditional statement
?>
Footnotes:
Check your server logs.
mail() doesn't work on localhost without an external library.
Also, you need to do an isset on $_POST['name'];
if (isset($_POST)){
$name = $_POST['name'];
$to = "email#email.com";
$subject = "this";
mail ($to, $subject, $name);
}
Also don't forget to look at headers!
Make this work for you first:
<?php
mail("Lennyperez#mail.com", "add this", "name");
, and ask question again.
Do the following: until the form is not submitted, the $_POST['name'] variable is not set
<?php
if (isset($_POST)) {
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
}
?>
When the page is loaded , it doesn't find post variables and that's normal because you have not clicked the submit button,your $_POST['name'] will exist only after the submit button is pressed.
Solution 1:
Add a condition , mail will be sent only if a button submit is clicked.
<?php
if (isset($_POST['name'])) {
$name = $_POST['name'];
$to = "Lennyperez#mail.com";
$subject = "add this";
mail ($to, $subject, $name);
}
?>
Solution 2:
Separate HTML file containing the form from Your action page
I am assuming you are receiving undefined notice if so please fallow one of these steps.
Add #$name= $_POST['name'];
OR
error_reporting(E_ALL ^ E_NOTICE);
Hello Fellow Stackers,
New to PHP and I am putting together a multipage form from pre-built code.
Basically the user selects as many checkboxes as they want... then the form submits to this secondary page. This secondary page echo's the checkboxes they chose at the top of the page via $check.. then they can enter their contact information and all of the information gets submitted via form, along with the $check information.
Everything is working perfectly except $check isn't being entered into the form message, but it works up at the top of the page, displaying which options the user inputted.
Any help is appreciated!
<?php
$emailOut = '';
if(!empty($_POST['choices'])) {
foreach($_POST['choices'] as $check) {
echo $check; //echoes the value set in the HTML form for each checked checkbox.
//so, if I were to check 1, 3, and 5 it would echo value 1, value 3, value 5.
//in your case, it would echo whatever $row['Report ID'] is equivalent to.
$emailOut .= $check."\n"; //any output you want
}
}
$errors = '';
$myemail = 'test#myemailHERE.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. $check ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message \n $emailOut";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
The thing in this situation is that when you get down to the email, $check is the last option displayed. You need to use the foreach statment to build the array or email output such as
$emailOut = "";
foreach($_POST['choices'] as $check) {
$emailOut .= $check."\n"; //any output you want
}
Then use your email variable in the same way
$email_body = "You have received a new message. Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message \n $emailOut";
UPDATE
From further investigation and more code submitted, it appears that you are working with a multi-form submssion issue. The issue is you have form 1 (checkboxes) that submits to form 2 (email).
Since when doing the checks after the checkbox submission, no name, email, etc was given so $errors were given and no email sent. When filling out the email form, the checkboxes were not sent again so $check or even $_POST['choices'] had values.
You can either put the two forms into one, or you can look into a way to save the values by passing them and filling a 'hidden' field (<input type='hidden' value='...'>) or use a session with PHP.
I have a single page portfolio website that I'm building and I have a contact form that I need to process using php send script. I'm a novice when it comes to PHP so I'm having trouble getting this to work. I've done some searching but I can't find what I'm looking for.
Here's what I have done, I copied this from a PHP contact page that I had built but the PHP and form are on the same page and I need an external send.php to process my form.
<?php
$error = ''; // error message
$name = ''; // sender's name
$email = ''; // sender's email address
$company = ''; // company name
$subject = ''; // subject
$comment = ''; // the message itself
if(isset($_POST['send']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
if($error == '')
{
if(get_magic_quotes_gpc())
{
$message = stripslashes($message);
}
// the email will be sent here
// make sure to change this to be your e-mail
$to = "example#email.com";
// the email subject
// '[Contact Form] :' will appear automatically in the subject.
// You can change it as you want
$subject = '[Contact Form] : ' . $subject;
// the mail message ( add any additional information if you want )
$msg = "From : $name \r\ne-Mail : $email \r\nCompany : $company \r\nSubject : $subject \r\n\n" . "Message : \r\n$message";
mail($to, $subject, $msg, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
}
}
if(!isset($_POST['send']) || $error != '')
{
header("location: http://www.#.com/#contact");
}
?>
So for my form I want to have:
<form method="post" action="send.php" class="form">
I plan on using HTML5 and jQuery to validate the form, so I really only need the script to capture the info and send the email to a single address. After it sends I want the script to redirect back to the Contact page.
Edit:
I found a solution after spending a while on google.
http://www.website.com/#contact");
?>
For one thing, you haven't initialized the value for $message
$message = stripslashes($message);
You probably meant to use $comment instead of $message
Not sure what problem you're facing. Simply copy the PHP you have into a file called send.php and my first glance says it'll work if you change $message back to $comment and add error checking. If you still have issues, post back with more details.
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 />
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
}
}