1

I have the following jQuery

        $("button#submit").click(function(){
            $.ajax({
                type: "POST",
                url: "process.php",
                data: $('form.contact').serialize(),
              success: function(msg){
                $("#form-content").modal('hide');                     
                $("#thanks").html(msg);
                $("#thanks").delay(2000).fadeOut("slow");
              },
                error: function(){
                    alert("failure");
                }
            });
        });

And Php

<?php
    if (isset($_POST['name'])) {
    $name = strip_tags($_POST['name']);
    $email = strip_tags($_POST['Email']);
    $sug = strip_tags($_POST['sug']);

    echo "<span class='label label-info'>Your Website has been submitted .. Thank you</span>";
}?>

This works the first time and displays the php echo on my page. But when I submit the form again it does not show.

2
  • Your $("#thanks") is hidden Commented Apr 1, 2013 at 4:08
  • its better if u can put this in a jsfiddle Commented Apr 1, 2013 at 4:09

3 Answers 3

5

Your $("#thanks") dom is hidden.

    $("button#submit").click(function(){
        $.ajax({
            type: "POST",
            url: "process.php",
            data: $('form.contact').serialize(),
          success: function(msg){
            $("#form-content").modal('hide');                     
            $("#thanks").html(msg);
            $("#thanks").show();  <----------------ADD THIS
            $("#thanks").delay(2000).fadeOut("slow");
          },
            error: function(){
                alert("failure");
            }
        });
    });
Sign up to request clarification or add additional context in comments.

Comments

2

Use .on().

$(document).on('click','button#submit',function(){  })

6 Comments

There is no difference between the on('click') and click.
@undefined i am afraid bro but there is....on('click') word as delegate and work on dynamically loaded content.
No, this not how on is used for event delegation, it's just a regular handler.
@undefined as far as i know its behave like i said....if i am wrong please correct me so i will not make mistake in future
$('#staticElement').on('click', '.dynamicallyGeneratedElement', function(){})
|
-1

use like this

$("button#submit").live('click', function(e) {
  $.ajax({
            type: "POST",
            url: "process.php",
            data: $('form.contact').serialize(),
          success: function(msg){
            $("#form-content").modal('hide');                     
            $("#thanks").html(msg);
            $("#thanks").delay(2000).fadeOut("slow");
          },
            error: function(){
                alert("failure");
            }
        });
    });

check difference here .live and .on jQuery .live() vs .on() method for adding a click event after loading dynamic html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.