Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Use PHP to send a contact form email only once

Posted on 2010-01-12
7
Medium Priority
?
504 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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

579 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