簡體   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