PHP联系表格-发送后希望保留在我的网站上 [英] PHP Contact Form - Want to stay on my site after send

查看:63
本文介绍了PHP联系表格-发送后希望保留在我的网站上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个PHP联系人表格,我所拥有的只是我的php脚本的小问题,即当电子邮件发出新的谢谢时页面被调用。因此带有联系表单的实际站点消失了,但是我不想发生这种情况。

如果点击了发送按钮,我想停留在我的站点上,显示一个空白的联系表,可能在联系表的下方仅1行,说谢谢.....

我该如何做到这一点?是否有任何代码片段可以向我解释我必须包含在html和php文件中的内容?希望它会...下面是我的php现在如何结束。

I am in the process of creating a PHP contact form and all I have is that little problem, with the php script I have, that when the email was send out a new "Thank you" page is called.So the actual site with the contact form disappears BUT I DON`T WANT THAT HAPPEN.

If the send button is hit I want to stay on my site, showing an empty contact form and maybe below the contact form just 1 line, saying "Thank you.....".

How can I make that happen? Is there any code snippet out there that can explain to me what I have to include to my html and to my php file? Hopefully it will...Below is how my php ends right now.

// send Email 
if (@mail( $empfaenger, htmlspecialchars( $betreff ), $mailbody, $mailheader ))
{
    // if email was successfully send
    echo 'Thank you for your Email. We will get in touch with you very soon.';
}

编辑

@ FreekOne

当前,我对代码进行了一些修改,因为我想让谢谢和/或错误面板滑出并使文本淡入。脚本正在接受我的代码(因为它是仍在工作),但实际上我看不到该文本实际上已经淡入。我看到了带有淡入淡出的滑动面板的示例。因此,这似乎是我编码的一种错误类型。
如果需要,请在此处查看代码:
http://jsbin.com/ohuya3
也许您可以为我指明正确的方向。当然,在这里的所有人会为您提供帮助。

EDIT
@FreekOne
Currently I am using your code with a slight modification because I wanted to make the thank you and or error panel make slide out and have the text fade in. The script is accepting my code (because it is still working) but actually I can not see that the text actually fades in. I have seen samples of sliding panels with fading in text. So it seems to be a wrong kind of coding that I did.
Please view the code here if you want:
http://jsbin.com/ohuya3
Maybe you can point me to the right direction. Of course, help would be appreciated from all of you guys here around.

推荐答案

设置表单以将数据发送到同一页面,并让您的脚本侦听提交。像这样的东西:

Set the form to send the data to the same page, and have your script listen for a submit. Something like:

contact.php

<?php
// Check if form was previously submitted
if(isset($_POST['myFormSubmitted'])) {
    // Do your form processing here and set the response
    $response = 'Thank you for your Email. We will get in touch with you very soon.';
}
?>
<!-- HTML here -->
<?php
if (isset($response)) { // If a response was set, print it out
    echo $response;
}
?>
<form method="POST" action="contact.php">
    <!-- Your inputs go here -->
    <input type="submit" name="myFormSubmitted" value="Submit">
</form>
<!-- More HTML here -->






UPDATE

考虑所提供的额外信息,我将通过AJAX亲自使用jQuery。首先,设置表单和结果容器:

Considering the provided extra info, I would personally do it with jQuery, through AJAX. First, setup your form and the container for the result:

HTML

<form id="myForm" method="POST" action="contact.php">
    <input type="text" id="name" name="name">
    <input type="text" id="email" name="email">
    <input type="text" id="message" name="message">
    <input type="submit" name="myFormSubmitted" value="Submit">
</form>
<div id="formResponse" style="display: none;"></div>

然后设置php脚本,该脚本处理提交的数据并输出响应。

Then setup the php script which handles the submitted data and outputs the response.

PHP(contact.php)

<?php
if(isset($_POST['myFormSubmitted'])) {
    // Do your form processing here and set the response
    echo 'Thank you for your Email. We will get in touch with you very soon.';
}
?>

最后,jQuery脚本将提交您的表单而无需离开页面并将结果插入到您的结果容器(效果不错,且效果简单)。

And finally, the jQuery script which will submit your form without leaving the page and insert the result in your result container (with a nice and simple fade in effect).

jQuery

$("#myForm").submit(function() {
    $.post('contact.php', {name: $('#name').val(), email: $('#email').val(), message: $('#message').val(), myFormSubmitted: 'yes'}, function(data) {
        $("#formResponse").html(data).fadeIn('100');
        $('#name, #email, #message').val(''); /* Clear the inputs */
    }, 'text');
    return false;
});

希望这会有所帮助!

这篇关于PHP联系表格-发送后希望保留在我的网站上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆