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
}
}
Related
I should preface this post with the fact that I am just a hobbyist and am not real code savvy when it comes to PHP and JQuery and such. What I am trying to do is send an email from an HTML form using a PHP script that I found on the internet somewhere. I have this all working just fine, but I dont like the fact that after the email is sent, the user is sent to a new page with a message saying it was sent. Instead of opening a new page, I would like the user to stay on the same page and get a popup message instead. Here is the code I am using for my PHP script:
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$subject = "Mail from website";
$message = $_POST['message'] ;
$to = "johndoe#gmail.com";
$headers = "From:" . $email . "\r\n" .
"Reply-To:" . $email;
if(mail($to,$subject,$message, $headers)) {
echo "Your email was sucessfully sent!";
} else {
echo "The email message was not sent.";
}
?>
I did not attach my html since I didnt think it was really needed. Any help on this would be greatly appreciated!
Redirect to a query string like header("location: formfile?msg=success");
And then in order to pull message just use $parts = parse_url($url); parse_str($parts['query'], $query); $alert_value = $_GET['msg']; in file where your form is located. After that you can do actions with if statements.
Here is example:
the file where form is located
<?php
$parts = parse_url($url);
parse_str($parts['query'], $query);
$alert_value = $_GET['msg'];
if ($alert_value == "success") {
//do your actions to show lighbox.
}
?>
<!-- Your code for form in html --->
And your file where you send email:
<?php
ini_set( 'display_errors', 1 );
error_reporting( E_ALL );
$name = $_POST['name'] ;
$email = $_POST['email'] ;
$subject = "Mail from website";
$message = $_POST['message'] ;
$to = "johndoe#gmail.com";
$headers = "From:" . $email . "\r\n" .
"Reply-To:" . $email;
if(mail($to,$subject,$message, $headers)) {
header("location: formfile.php?msg=success");
} else {
header("location: formfile.php?msg=error");
}
?>
I want to fade out php echo message. Here is my php code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$subject = $_POST['subject'];
$to = 'ajay.k#enexl.com';
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { // this line checks that we have a valid email address
$mailSubject = "Contact request from " .$name;
$txt = "name : ".$name.".\n\nSubject : ".$subject.".\n\nMail id : ".$email."\n\nMessage : ".$message;
$headers = "From: ".$email ;
mail($to,$mailSubject,$txt,$headers);
$data = array();
$data['status'] = 'success';
//echo json_encode($data);
echo "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>";
echo "<p id='#text'>Your email was sent! One of our team members would contact you shortly!</p>"; // success message
echo "<script type='text/javascript'>";
echo "$(function(){";
echo "$('#text').fadeOut(5000);";
echo "});";
echo "</script>";
}
else{
echo "Mail was not sent, make sure that all fields are filled in";
}
?>
When the form data gets submitted, successfully, I get the response as Your email was sent! One of our team members would contact you shortly!. However it doesn't fade out as expected. How can I make it fade out?
Dont use # in you id qualifier. #text says to find an element with id text not #text
echo "<p id='#text'>Your email was sent! One of our team members would contact you shortly!</p>"; // success message
should be
echo "<p id='text'>Your email was sent! One of our team members would contact you shortly!</p>"; // success message
I am new to php and I am having issues with the php mail function. I am testing on a live server, not localhost. When I complete my form I get no response from the mail function. I'm not sure what i'm doing wrong.
<?php
if (($_SERVER['REQUEST_METHOD'] == 'POST') && (!empty($_POST['action']))):
if (isset($_POST['fullname'])) { $fullname = $_POST['fullname']; }
if (isset($_POST['email'])) { $email = $_POST['email']; }
if (isset($_POST['phone'])) { $phone = $_POST['phone']; }
if (isset($_POST['form_message'])) {
$form_message = filter_var($_POST['form_message'], FILTER_SANITIZE_STRING); }
$form_errors = false;
if( $fullname === '') :
$form_errors = true;
endif;
if ($form_message === '') :
$form_errors = true;
endif;
if (!$form_errors) :
$to = "email#address.com";
$subject = "From $fullname --VTS Specialist Contact Form";
$message = "$form_message";
$replyto = "From: $email \r\n" .
"Reply-To: email#address.com";
if(mail($to, $subject, $message)):
$msg = "Thanks for reaching out to VTS Specialist, We will get back to you as soon as possible!";
else:
$msg = " Sorry your message could not be sent, try again.";
endif; # mail form data
endif; #check for form errors
endif;
?>
Try adding an echo statement at the bottom - as Mario mentions, you are only declaring it, you're not displaying it.
if(mail($to, $subject, $message)):
$msg = "Thanks for reaching out to VTS Specialist, We will get back to you as soon as possible!";
else:
$msg = " Sorry your message could not be sent, try again.";
endif; # mail form data
echo $msg;
I'm trying to make a php mail form display a success message upon a successful submit.
I'm new to php and I'm not sure how to make this work.
As it sits now, after submit, the site just reloads at the top of the page. I'd like it to refresh at the contact div and display a success message.
Contact div html:
<div class="contact-wrapper" >
<div class="contact">
<div class="contact-left" id="contact">
<?php
if (isset($_REQUEST['email'])) {
echo "Thank you for your message.";
}
$mail_form = include('php/mail_form.php'); ?>
</div> <!-- end div contact -->
Contact from PHP:
<?php
function spamcheck($field)
{
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
if (isset($_REQUEST['email']))
{//if "email" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['email']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("idc615#gmail.com", "Subject: $subject",
$message, "From: $email" );
header("location: index.php");
}
}
?>
Try this code... You need to halt your script for some time so that user can see success or failure message. Without sleep user will not be able to see message as header will execute immediately.
if(mail("idc615#gmail.com", "Subject: $subject",$message, "From: $email" )){
echo "Thank you for your message";
sleep(2); // sleep for 2 seconds. Enough to display success message
//header will execute after 2000ms(2s)
header("location: index.php");
}else{
echo "Unable to send message";
sleep(2); // sleep for 2 seconds
header("location: index.php");
}
or try code given below:
in Contact from PHP
if(mail("idc615#gmail.com", "Subject: $subject",$message, "From: $email" )){
header("location: index.php?status=1");
}else{
header("location: index.php?status=0");
}
in index.php
if(isset($_GET['status'])){
$status = $_GET['status'];
if($status == 1){
echo "Thank you for your message";
}else if($status == 0){
echo "Unable to send message";
}
}
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$message = mysql_real_escape_string($_POST['message']);
if(isset($_POST['contactrgtr']))
{
$query = "insert into contact_us(name,email,message)values('$name','$email','$message')";
$res = mysql_query($query);
if($res){
$message = 'Your Message Successfully Sent';
include_once('contact.php'); } `
Then in contact.php page you have to define <?php $message ?>.
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);