首页 > 解决方案 > 提交 PHP 电子邮件表单后,在同一页面上提醒“消息已发送”而不是页面重定向。

问题描述

我的网站上有一个联系表单,其中包含来自 freecontactform.com 的代码,该表单提供了下面的 PHP 代码。我在脚本底部添加了我自己的 HTML,以便在提交表单后创建一种启动页面,但是我更希望在同一页面上出现警报,例如“已发送消息”,而无需重定向。有人可以告诉我正确的代码来实现我正在寻找的同一页面警报吗?

    <?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "ts95studios@gmail.com";
    $email_subject = "RE: TS95Studios.com";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['subject']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $phone = $_POST['subject']; // not required
    $message = $_POST['message']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$name)) {
    $error_message .= 'The Name you entered does not appear to be valid.<br />';
  }

  if(strlen($message) < 2) {
    $error_message .= 'The message you entered do not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Subject: ".clean_string($phone)."\n";
    $email_message .= "Message: ".clean_string($message)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

<meta name="viewport" content="width=device-width, initial-scale=1">

<link href="http://ts95studios.com/css/TS95StudiosMainCSS.css" rel="stylesheet" type="text/css">
<link href="http://ts95studios.com/css/codepen.css" rel="stylesheet" type="text/css">

<body class="success">
<div class="success">
<div class="successinner">
<img src="http://ts95studios.com/images/TS95Logo.png" class="ts95logo">
<h1 class="author fontwhite">Message Sent!</h1>
    <p class="pageintro fontwhite">Thank you for reaching out, I will be in touch soon.</p>
    <a href="http://ts95studios.com" class="contactmelinkb"><button class="actionbutton">BACK TO SITE</button></a>
  </div>
</div>
</body>

<?php

}
?>

标签: phpformsemailalert

解决方案


你真的有两个选择。

  1. 重新加载当前页面,处理消息,然后显示结果。
  2. 使用AJAX将消息的主体发送到后台的不同脚本,在不重新加载页面的情况下返回结果。

对于选项 1,您可以执行以下操作:

<?php
$email_sent = false;
if(isset($_POST['email'])) {

    ## Process the email here

    $email_sent = true;

}

?>

<form action="{{THIS SAME FILE}}" method="post">
...
</form>

<? if($email_sent) { ?>
Thanks for contacting me/us/whoever!
<? } ?>

对于选项 2,这里是 W3Schools 的 AJAX 和 PHP 示例。本质上,使用 Javascript,您将调用您的电子邮件发送脚本,将表单中的所有信息提供给它,然后接收您的脚本输出的任何内容。


推荐阅读