0

These form work after submission .but after refreshing the page the mail send again and again.

<?php
$to = "abc.com";
$subject = "This is subject";
$message = "<b>This is HTML message.</b>";
$message .= "<h1>This is headline.</h1>";
$header = "From:mithlesh@rightturn.co.in \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html\r\n";
$retval = mail ($to,$subject,$message,$header);
if( $retval == true ) {
    echo "Message sent successfully...";
}else {
    echo "Message could not be sent...";
}
?>
executable
  • 3,365
  • 6
  • 24
  • 52
mithlesh
  • 27
  • 5

3 Answers3

0

You need to check submit

if (isset($_POST['submit'])) {
}

So your code is :

if (isset($_POST['submit'])) {
    $to = "abc.com";
    $subject = "This is subject";
    $message = "<b>This is HTML message.</b>";
    $message .= "<h1>This is headline.</h1>";
    $header = "From:mithlesh@rightturn.co.in \r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";
    $retval = mail ($to,$subject,$message,$header);
    if( $retval == true ) {
       echo "Message sent successfully...";
    }else {
       echo "Message could not be sent...";
    }
}
Siddharth Ramani
  • 661
  • 3
  • 12
-1

Mostly mail sending happens after particular event or activity just like form-submit or button-click.. you need to design form with relevant fields.. then you can use isset() or !empty() for $_POST array

if(!empty($_POST)){
    $to = "abc.com";
     $subject = "This is subject";
      $message = "<b>This is HTML message.</b>";
     $message .= "<h1>This is headline.</h1>";
     $header = "From:mithlesh@rightturn.co.in \r\n";
     $header .= "MIME-Version: 1.0\r\n";
     $header .= "Content-type: text/html\r\n";
     $retval = mail ($to,$subject,$message,$header);
     if( $retval == true ) {
        echo "Message sent successfully...";
     }else {
        echo "Message could not be sent...";
     }
}
Rakesh P
  • 438
  • 5
  • 19
-1

put this code in starting of your php code outside isset submit button

if(empty($_SESSION['key']) && !isset($_SESSION['key'])){
  $randomkey = rand(0,99999);
  $mykey = $_SESSION['key'] = $randomkey
}

It set Session a unique random key. now in form field take one hidden field

<input type="hidden" name="key" value="<?=$mykey?>">

Now On Submit

First Check Current $mykey Matches With Hidden Variable After Performing Certain Action Unset $mykey So it prevent user adding data multiple times by Refreshing the page.

I hope it Helps... :-)

TarangP
  • 2,711
  • 5
  • 20
  • 41