繁体   English   中英

提交PHP电子邮件表单后,在同一页面上发出“消息已发送”警报,而不是页面重定向。

[英]Alert 'Message Sent' on the same page instead of page redirect after submitting PHP email form.

我的网站上有一个联系表单,其中包含来自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

}
?>

您确实有两个选择。

  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,您将调用电子邮件发送脚本,从表单中提供所有信息,并接收脚本输出的任何内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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