-1

我在我的网站上创建了一个 PHP 联系表单,它运行良好,除了看到将用户发送到thank-you.php 页面的成功或错误消息。这就是我设置它的方式,但我希望用户提交并且表单应该消失并说谢谢!代替表格。

这是我的表单 HTML:

<form class="form-foot" action="thank-you.php" method="post">
<fieldset>
<legend>Footer contact form</legend>
<div class="field">
<label for="name">Your name: </label>
<input name="name" id="name" type="text" placeholder="Name" class="required field-name" required>
</div>
<div class="field">
<label for="email">Your email: </label>
<input name="email" id="email" type="email" placeholder="Email address" class="required  field-email" required>
</div>
<div class="field">
<label for="message">Message: </label>
<textarea name="message" id="message" cols="30" rows="8" class="required field-message" placeholder="Your message" required></textarea>
</div>
<div class="field-submit">
<input class="sendbutton" type="submit"/>
</div>
</fieldset>
</form>

这是我在thank-you.php页面中的PHP:

<?php $name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$formcontent="From: $name \n Message: $message";
$recipient = "myemail@mysite.com";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

如何使表单在​​提交时消失并回显“谢谢您”成功消息,而无需访问thank-you.php 或刷新页面。

谢谢

4

2 回答 2

0

Replace:

<form class="form-foot" action="thank-you.php" method="post">

with:

<form name="myform" class="form-foot" action="" method="post">

Give a name to your submit button:

<input name="submit" class="sendbutton" type="submit"/>

Move the code from thank-you.php to yourform.php:

if(isset($_POST['submit']))
{

    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $formcontent="From: $name \n Message: $message";
    $recipient = "myemail@mysite.com";
    $subject = "Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Thank You!";
}
于 2013-06-04T09:41:54.347 回答
0

您可以使用 ajax 从 javascript 将数据发布到thank-you.php 文件。通过在提交时调用函数而不是表单中的 action 属性。该函数可以具有以下格式

var url = "thank-you.php";
var data = $('#your-form-id').serialize();


$.ajax({
        type: "post",
        url: url,
        data: data,
        success: function(msg) {

        }    
   });

从感谢页面获取消息后,您可以使用它为它分配包含表单的 html,以将表单替换为谢谢!

于 2013-06-04T09:43:50.887 回答