最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - jQuery - replace contact form with 'thanks message' - Stack Overflow

programmeradmin2浏览0评论

I have a contact form which performs a PHP action. The contact form is connected with validation engine in jQuery. If messege is sent correctly I simply include PHP file with thanks message - require_once('success.php');. After sending message I would like to replace contact form with thanks message without reloading the whole page. Please give me some advices how to do it. Here is my html:

<div id="contactForm">
<form id="expertForm" class="formular" method="post" action="send.php">
    <fieldset>
        <label>
            <input name="email"
                   id="email"
                   class="required email"
                   type="text"
                   size="40"/>
        </label>

        <p>
            <textarea name="body" id="body" rows="5" cols="50" class="required"></textarea>
        </p>
    </fieldset>
    <input class="submit"
           type="image"
           src="../images/btn-send.png"/>
</form>
</div>
<script type="text/javascript">
  $("#expertForm").validate();
</script>

In send.php I have:

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    require_once('success.php');
}

I have a contact form which performs a PHP action. The contact form is connected with validation engine in jQuery. If messege is sent correctly I simply include PHP file with thanks message - require_once('success.php');. After sending message I would like to replace contact form with thanks message without reloading the whole page. Please give me some advices how to do it. Here is my html:

<div id="contactForm">
<form id="expertForm" class="formular" method="post" action="send.php">
    <fieldset>
        <label>
            <input name="email"
                   id="email"
                   class="required email"
                   type="text"
                   size="40"/>
        </label>

        <p>
            <textarea name="body" id="body" rows="5" cols="50" class="required"></textarea>
        </p>
    </fieldset>
    <input class="submit"
           type="image"
           src="../images/btn-send.png"/>
</form>
</div>
<script type="text/javascript">
  $("#expertForm").validate();
</script>

In send.php I have:

if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    require_once('success.php');
}
Share Improve this question edited Oct 6, 2011 at 7:58 Muhammad Usman 12.5k6 gold badges38 silver badges60 bronze badges asked Oct 5, 2011 at 12:03 mkasmkas 5132 gold badges6 silver badges15 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

You can see an almost working demo here http://jsfiddle/v7MJA/1/

$(function(){
    $("#expertForm").submit(function(e){
        e.preventDefault();
        if(!$(this).validate().form()) return false;
        $.ajax({
            url:$(this).attr('action'),
            data:$(this).serialize(),
            type:'post',
            success:function(msg){
                $("#expertForm").replaceWith(msg);
            }
        });
    });
});

You'd better give us some code so that we could help.

Here is the theorical way: use the success event of the jquery ajax[ref] call:

$.ajax({
  url: "test.html",
  context: document.body,
  success: function(){
    $("#myformdiv").html("Thanks!");
  }
});

assuming that your HTML markup is something like:

<div id="myformdiv">
   <form> 
     <!-- form code here -->
   </form>
</div>

Assuming I've understood your question correctly, you can use the replaceWith method to replace the matched elements with the specified content:

$("#yourForm").replaceWith("<p>Thanks!</p>");

I'm assuming that you're doing something asynchronously to send the form data to the server, so you can just run the above code in the callback:

$.post("yourScript.php", function() {
    $("#yourForm").replaceWith("<p>Thanks!</p>");
});
发布评论

评论列表(0)

  1. 暂无评论