简体   繁体   中英

showing a successful alert message after sending an email

This is an email sending code

mail($ToID,$subject,$urltoemail,$headers);

after sending, the user is redirected

header("Location:".$_SERVER['SCRIPT_NAME']);

I have to show an alert of "mail has been send successfully" in the redirected page. How can I?


Excerpt of code:

if(isset($_POST['submitbtn'])) {
  $flag=true; $error="";
  /*Getting Values**********************/
  $ToID=$_POST['ToID'];
  $fromID=$_POST['fromID'];
  $subject=$_POST['subject'];
  $URL=$_POST['URL'];
  $message=$_POST['message'];
  /*Validating the data*****************/
  if(strlen($ToID)==0) {
    $flag=false;
    $error.="Enter To email address field";
  } elseif(!preg_match_all("|^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$|U", $ToID, $arr)) {
  $flag=false; $error.="Invalid To email address";
  }

  if($flag) {
    $urltoemail = "<html><head><title>Forgot Password</title></head><body> <table><tr><td>".$message."</td></tr><tr><td><a href='{$URL}'>Click here for viewing map</a></td></tr></table></body></html>";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    if(strlen($fromID)==0)
        $fromID = 'info@cimaps.co.uk';
    $headers .= 'From: '.$fromID. "\r\n";
    mail($ToID,$subject,$urltoemail,$headers);
    //$_SESSION['m'] = "item successfully deleted";
    header("Location:".$_SERVER['SCRIPT_NAME']);
  }
}

You could try include a variable in the redirected URL, triggering an alert on the page. Use

header("Location: ".$_SERVER['SCRIPT_NAME']. "?sent=1");

and then include this somewhere:

if ($_GET["sent"]) {
    echo '<script>alert("The email has been sent successfully.");</script>';
}

If you are willing to change the structure of your code a bit more you could

  • redirect to a new page, which contains the alert code.
  • just show the alert directly on the current page, without reloading with a redirect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM