PHP DIV 中的联系表单状态更新

PHP Contact Form Status Update in DIV

听起来是个奇怪的问题,但这就是我的问题。 php 末尾有一个片段,上面写着成功或失败会导致白屏显示,我真的希望它在同一页面的提交按钮下方弹出,而不是重定向整个页面。

我假设我可以将它指向一个空的 <div>

mail.php:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $formcontent=" From: $name \n Phone: $phone \n Message: $message";
    $recipient = "art@jamisonsigns.com";
    $subject = "Web Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Thank You!" . " -" . "<a href='index.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>

在我的网站上用一个简单的HTML表格如下:

<form class="contact-form" form action="mail.php" method="POST">
      <fieldset>
           <input type="text" class="name" id="name" placeholder="Name...">
      </fieldset> 
      <fieldset>
           <input type="email" class="email" id="email" placeholder="Email...">
      </fieldset> 
      <fieldset>
           <input type="text" class="phone" id="phone" placeholder="Phone...">
      </fieldset>
      <fieldset>
           <textarea name="message" id="message" cols="30" rows="4" placeholder="Message.."></textarea>
      </fieldset>
      <fieldset>
           <input type="submit" class="button" id="button" value="Send Message">
      </fieldset>
 </form>

您需要提交带有 Ajax 的表格。然后在成功时将消息插入 html.

$('#button').click(function(event) {
event.preventDefault();

var formdata = $('#formID').serializeArray();
var url = '/urltopost.php';

$.ajax( 
{
    url : url,
    type: "POST",
    data: formdata,
    success:function(data, textStatus, jqXHR) 
    {
        //data: return data from server
        console.log("STATUS:" + textStatus + " | jqXHR:" + jqXHR + " | data:" + data);
    },
    error: function(jqXHR, textStatus, errorThrown) 
    {
        //if fails      
        alert("ERROR:" + " -> " +errorThrown);
    }
});

});

Here it is a tutorial if you need..