开发者

PHP Contact Form - Want to stay on my site after send

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 ap开发者_如何学运维preciated 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

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 !


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜