PHP 联系表 - 发送后希望留在我的网站上

发布于 2024-09-26 09:03:02 字数 852 浏览 1 评论 0原文

我正在创建一个 PHP 联系表单,我所遇到的只是一个小问题,即我的 php 脚本在发送电子邮件时会调用一个新的“谢谢”页面.因此,带有联系表单的实际网站消失了,但我不希望这种情况发生。

如果点击发送按钮,我想留在我的网站上,显示一个空的联系人表格,也许在联系表格下方只有 1 行,说“谢谢......”

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

// 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
也许你可以给我指出正确的方向。当然,我们将不胜感激这里所有人的帮助。

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.';
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-10-03 09:03:02

设置表单以将数据发送到同一页面,并让您的脚本侦听提交。类似于:

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 来完成此操作。首先,设置表单和结果容器:

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 脚本来处理提交的数据并输出响应。

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 脚本将在不离开页面的情况下提交表单,并将结果插入结果容器中(具有漂亮且简单的淡入淡出效果)。

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;
});

希望这有帮助!

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

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>

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.';
}
?>

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;
});

Hope this helps !

沫雨熙 2024-10-03 09:03:02

下面所有这些答案都是错误的。它有时会让您收到重复的垃圾邮件,并使客户感到困惑。

虽然解决方案有点棘手

首先你必须学习黄金法则:
处理 POST 请求后,您的代码应使用 GET 方法重定向浏览器。没有例外。

因此,首先要像这样,

if ($_SERVER['REQUEST METHOD']=='POST') {
  mail( $empfaenger, htmlspecialchars( $betreff ), $mailbody, $mailheader );
  header("Location: ".$_SERVER['PHP_SELF']);
  exit;
}  
// your form goes here

无论如何你都应该这样做。

接下来,如果你仍然想显示这个无用的消息,你有几种方法可以做到。

  • 使用 GET 参数
  • 使用 cookie
  • 进行 AJAX。

例如,使用 GET 参数

if ($_SERVER['REQUEST_METHOD']=='POST') {
  mail( $empfaenger, htmlspecialchars( $betreff ), $mailbody, $mailheader );
  header("Location: ".$_SERVER['PHP_SELF']."?thanks");
  exit;
}  
if ($_SERVER['QUERY_STRING']=='thanks') {
  echo 'Thank you for your Email. We will get in touch with you very soon.';
}
// your form goes here

All these answers below are wrong. It will make you occasionally spammed with doubled messages and confuse customers.

Though the solution is kinda tricky

First of all you have to learn the Golden rule:
After processing POST request, your code should redirect a browser using GET method. No exceptions.

Thus, first make it like this

if ($_SERVER['REQUEST METHOD']=='POST') {
  mail( $empfaenger, htmlspecialchars( $betreff ), $mailbody, $mailheader );
  header("Location: ".$_SERVER['PHP_SELF']);
  exit;
}  
// your form goes here

You ought to do it anyway.

Next, if you still want to show this useless message, you have several ways to do it.

  • using GET parameter
  • using cookie
  • ising AJAX.

for example, to use a GET parameter

if ($_SERVER['REQUEST_METHOD']=='POST') {
  mail( $empfaenger, htmlspecialchars( $betreff ), $mailbody, $mailheader );
  header("Location: ".$_SERVER['PHP_SELF']."?thanks");
  exit;
}  
if ($_SERVER['QUERY_STRING']=='thanks') {
  echo 'Thank you for your Email. We will get in touch with you very soon.';
}
// your form goes here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文