Php 如何创建一个弹出式联系人表单,在发送按钮的位置返回成功消息?

Php 如何创建一个弹出式联系人表单,在发送按钮的位置返回成功消息?,php,html,Php,Html,我有一个HTML弹出的联系表单,我需要帮助在页面上或在发送按钮的位置返回一条成功消息 <div class="form-popup" id="myForm"> <form id="myForm" action="action_page.php" method="post" enctype="multipart/form-data" class="form-container"> <h1>Contact</h1> <

我有一个HTML弹出的联系表单,我需要帮助在页面上或在发送按钮的位置返回一条成功消息

<div class="form-popup" id="myForm">
   <form id="myForm" action="action_page.php" method="post" enctype="multipart/form-data" class="form-container">
      <h1>Contact</h1>
      <input type="text" placeholder="Name" name="name" required>
      <input type="text" placeholder="Email" name="email" required>
      <textarea rows="4" cols="33.5" placeholder="Your message..." name="message" required tabindex="35"></textarea>
      <button type="submit" class="btn">Send</button>
      <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
   </form>
</div>



  <?php
    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $message = $_POST['Message'];
    $from = 'From: mywebsite.com'; 
    $to = 'sk@gmail.com'; 
    $subject = 'Message from MyWebsite';

    $headers = 'From: mywebsite.com' . "\r\n" . 
    'Reply-To: ' . $email . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


    $body = "Name: $name\n Email: $email\n Message:\n $message";

?>

//please refer to my PHP code below that communicates with my HTML file.
currently after PHP submits my form it redirects to the homepage. I want 
<?php
if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $headers)) { 

        header("Location: http://www.mywebsite.com");


    } else { 
        echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
    }
}
?>

联系
邮寄
关
//请参考下面与HTML文件通信的PHP代码。
目前,PHP提交我的表单后,它会重定向到主页。我想要
它将保持在同一页面上,并在“发送”按钮中显示已成功发送的消息

<div class="form-popup" id="myForm">
   <form id="myForm" action="action_page.php" method="post" enctype="multipart/form-data" class="form-container">
      <h1>Contact</h1>
      <input type="text" placeholder="Name" name="name" required>
      <input type="text" placeholder="Email" name="email" required>
      <textarea rows="4" cols="33.5" placeholder="Your message..." name="message" required tabindex="35"></textarea>
      <button type="submit" class="btn">Send</button>
      <button type="button" class="btn cancel" onclick="closeForm()">Close</button>
   </form>
</div>



  <?php
    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $message = $_POST['Message'];
    $from = 'From: mywebsite.com'; 
    $to = 'sk@gmail.com'; 
    $subject = 'Message from MyWebsite';

    $headers = 'From: mywebsite.com' . "\r\n" . 
    'Reply-To: ' . $email . "\r\n" .
    'X-Mailer: PHP/' . phpversion();


    $body = "Name: $name\n Email: $email\n Message:\n $message";

?>

//please refer to my PHP code below that communicates with my HTML file.
currently after PHP submits my form it redirects to the homepage. I want 
<?php
if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $headers)) { 

        header("Location: http://www.mywebsite.com");


    } else { 
        echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
    }
}
?>

此解决方案要求JQuery运行

$('#myForm')。提交(函数(e){
//做你的事
e、 preventDefault()//如果要提交表单,请删除此行
$('#send').hide();
$('.success').show();
});

联系
成功信息
邮寄
关

您应该将php代码置于html之上,并检查请求是POST还是GET。标题(“位置:”);正在重定向到主页。而不是创建存储消息(成功或错误)的变量,如果请求是POST,则在表单上显示它


联系
邮寄
关

我还有一个php代码。我应该保留它并将您给我的代码添加到html文件中吗?PHP是在服务器端执行的。JS在客户端执行。只需添加,然后添加$('#myForm').sub……。我将您的JS脚本添加到我的HTML中。但它不起作用。不过有一个问题。成功消息显示正确,但未发送表单。我是否需要调整HTML与PHP的通信方式?目前,我的PHP是echo,在发送消息时直接指向主页。您应该删除e.preventDefault()以提交表单,如注释所示。请注意,代码始终显示成功消息,它实际上并没有检查发送是否成功。很抱歉,我不理解您的观点。我把你给我的所有代码都放在那里了,但它不起作用。