?
Solved

Use PHP to send a contact form email only once

Posted on 2010-01-12
7
Medium Priority
?
471 Views
Last Modified: 2013-12-13
I have a e-mail contact form written php and html.
On the first page quick_quote.php you input your contact data.
The quick-quote.php  page has the the e-mail script.
Once the submit button is pressed it directs you to the thank-you.php  page.   The thank-you.php page displays the information the client filed out on the previous page.
The variables are transfered from one page to another by $_POST.
My problem is if the client clicks refresh or if the client goes back to quick-quote.php page an e-mail, is sent.

How can I make it that if the client goes back or clicks refresh the client does not get another  e-mail.
//quick-quote.php


if(isset($_POST['submit'])) $email=$_POST['email'];
//echo "$email </br>";
$firstname=$_POST['firstname'];
//echo "$firstname </br>";
  $lastname=$_POST['lastname'];
//echo "$lastname </br>";

$business_name=$_POST['business_name'];



$to      = '';
$subject = ' ';
$message = "The estimated  $business_name 
$headers = 'From: ' . "\r\n" .
    'Reply-To: ' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

<input type="text" name="business_name" />
 
  First Name:<br />
  <input type="text" name="firstname" />
  <br />
  Last Name:<br />
  <input type="text" name="lastname" />
</p>
<p>E-mail Address:<br />
</p>
<input type="text" name="email" size="40" />
<p></p>
<label for="label"></label>
<p>
  <label for="business_name"></label>
  <br />
</p>
<p><input name="submit" type="hidden" value="submit" />
  <input type="submit" name="submit" />
</p>

</form>

//thank-you.php

>The estimated Rate for <?php echo "$business_name"; ?> </p>

Open in new window

0
Comment
Question by:tomjenkins12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26293090
Is this method called at the onload event of the HTML?
Or is it called from the script tag outside any function?

Thanks
0
 

Author Comment

by:tomjenkins12
ID: 26293115
Onload event

if(isset($_POST['submit']))
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26293158
If it at the onload event then these issues will happen.

You might want to submit on some other event.
0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 15

Accepted Solution

by:
ludofulop earned 2000 total points
ID: 26293255
after sending the email, redirect user to a thank  you page:

mail(...);
header('Location: index.php?act=thankyou');
exit();

or, use session:

if (!$_SESSION['mailsent'])
{
  mail();
  $_SESSION['mailsent'] = 1;
}

don't forget to reset somewhere the session value (user to be able to send another email).

i recommend you to use the first solutin (redirect)
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26293287
@ludofulop: Will it prevent the email from getting sent in case user comes back to the same page?
0
 
LVL 15

Expert Comment

by:ludofulop
ID: 26293390
@gurvinder372: no, you are right... but mostly i'm doing it this way, and have no problems (or maybe user lamas are avoiding my sites)

but OK, seems like session is better solution
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 26293610
The usual way to do this is either to

1) Use two scripts - one that does the sending of the emails and then on completion uses the header() function to redirect to a simple "thank you" page which does nothing other than display a "Thank you" message. They can press Reload until they are blue in the face and it will not resend

2) (a bit like the session thing above) Use the same page but make its default behaviour to display a "thank you" unless some parameters are passed to cause an email send. When the send is complete use the header() function to load the page with no parameters.

http://www.php.net/header

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question