Submitting a form using Ajax

I have a div created with the form inside and set to publish using ajax and returning the results in the div like this:

$(document).ready(function(){
    $("#guestList").validate({
        debug: false,
        submitHandler: function(form) {
            // do other stuff for a valid form
            //$('form').attr('id', 'guestList1')
            $.post('brides_Includes/guestlistDisplay1.php', $("#guestList").serialize(), function(data) {
                $('#results').html(data)
                $("form#guestList")[0].reset();
            });
        }
    });
});

When the results return, they show the correct changes and replace the form. However, when I submit the form again. Corresponding changes occur as they should, but then it refreshes the page and displays the posted information in the address bar.

How can I post a form and replace it so that it can post messages and call the script again if this does not happen?

+5
source share
1 answer

ajax , JavaScrip, , / . - JavaScrip HTML ajax.

, . ID (brides_Includes/guestlistDisplay1.php). , JavaScrip.

<script type="text/javascript">
$(document).ready(function(){
  $("#guestList").validate({
    debug: false,
    submitHandler: function(form) {
      // do other stuff for a valid form
      //$('form').attr('id', 'guestList1')
      $.post('brides_Includes/guestlistDisplay1.php', $("#guestList").serialize(), function(data) {
        $('#results').html(data);

        //This executes the JavaScript passed back by the ajax.
        $("#results").find("script").each(function(i) {
          eval($(this).text());
        });

        $("form#guestList")[0].reset();
      });
    }
  });
});
</script>
+3

All Articles