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
    – viclim
    Apr 1, 2013 at 4:08
  • its better if u can put this in a jsfiddle
    – Swarne27
    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");
            }
        });
    });
0
2

Use .on().

$(document).on('click','button#submit',function(){  })
6
  • There is no difference between the on('click') and click.
    – Ram
    Apr 1, 2013 at 4:30
  • @undefined i am afraid bro but there is....on('click') word as delegate and work on dynamically loaded content. Apr 1, 2013 at 4:32
  • No, this not how on is used for event delegation, it's just a regular handler.
    – Ram
    Apr 1, 2013 at 5:38
  • @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 Apr 1, 2013 at 5:40
  • $('#staticElement').on('click', '.dynamicallyGeneratedElement', function(){})
    – Ram
    Apr 1, 2013 at 5:41
-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

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.