-1

I have a form where you can send an email to someone, if i reload the page it sends anouther email. How do I make it so it does not do that? A forum said to change it to GET, that did not work either. mail function is not showing up.

<form action="index.php" method="GET">
<textarea name="comment" cols="16.5" rows="5" style="resize: none;" maxlength="240">            </textarea>
<input type="submit" value="Send Comment" />
<?php
if (isset($_GET['comment'])) {
$com = mysql_real_escape_string($_GET['comment']);
if (!empty($com)) {
if (strlen($com) <= 240 && strlen($com) >= 5) {                                         $mail = mail('__________', 'Comments', $com);
}
}
}
?>
</form>
  • A POST form with a redirect afterwards is a much better idea - the user can refresh afterwards and it will not send additional mails. – halfer Nov 30 '13 at 17:16

2 Answers2

2

Send your form with POST to another page: say upload.php, then in upload.php you add this code after you have checked the form:

header( 'Location: form_page.php' );

that way the form page can be reloaded without having this issue.

display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
  • when i hosted my website headers and sessions stop working, anyone know why? – Raymond Kneipp Nov 30 '13 at 17:20
  • 1
    Do you output anything (`echo` or similar) before `header( 'Location: form_page.php' );` ? – display-name-is-missing Nov 30 '13 at 17:22
  • @RaymondKneipp Strange.. Can you specify what you mean that the headers and sessions stopped working. What error did you get? – display-name-is-missing Nov 30 '13 at 17:29
  • Session Error:Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home3/thor3444/public_html/index.php:10) in /home3/thor3444/public_html/index.php on line 16 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home3/thor3444/public_html/index.php:10) in /home3/thor3444/public_html/index.php on line 16 – Raymond Kneipp Nov 30 '13 at 17:34
  • Header Error: Warning: Cannot modify header information - headers already sent by (output started at /home3/thor3444/public_html/attempt.php:8) in /home3/thor3444/public_html/attempt.php on line 36 – Raymond Kneipp Nov 30 '13 at 17:35
  • @RaymondKneipp About the session error: Have you made sure that the page source code begins with ` – display-name-is-missing Nov 30 '13 at 17:46
  • @RaymondKneipp You can take help from this SO-post about the Header error: http://stackoverflow.com/questions/8028957/headers-already-sent-by-php – display-name-is-missing Nov 30 '13 at 17:47
0

Add a GET parameter (say for eg: sent) to the link (index.php) and check if it is not set before sending the mail.

<form action="index.php?sent=true" method="GET">
  <textarea name="comment" cols="16.5" rows="5" style="resize: none;" maxlength="240"></textarea>
  <input type="submit" value="Send Comment" />
  <?php
  if (!isset($_GET['sent'])) {

     // Your code for sending the mail here...

  }
?>
</form>
Kevin
  • 6,539
  • 5
  • 44
  • 54