Don'tDownvoteMe
Don'tDownvoteMe

Reputation: 511

How to get a success message in this particular AJAX call?

I am using this particular way of sending the data via Ajax and I am not able to get a success message using this. Is there another way to send multiple data after doing $('form').serialize() and also get a success message back. The code that I am using is mentioned below.

<script>
    $(document).ready(function(){

        $('#sub').click(function(event){
            event.preventDefault();

            $.ajax({
                method:"POST",
                url:"main_class.php",
                data:$('form').serialize() + "&submit=submit"

            }); 
        });
    });    
    </script>

Upvotes: 0

Views: 2441

Answers (2)

rufatZZ
rufatZZ

Reputation: 832

You can easily use ajax success and error messages.

For example you can use this code snippet:

<script>
  $(document).ready(function(){

    $('#sub').click(function(event){
        event.preventDefault();

        $.ajax({
        dataType:"json",
        method:"POST",
        url:"main_class.php",
        success: function(data) {
            statusMsg = 'Data was succesfully captured';
            $("#successMsg ").text(statusMsg );
        },
        error: function(data) {
            statusMsg  = 'Error';
            $("#statusMsg ").text(statusMsg );
        },
    });

    return false;
</script>

Another way to solve your problem. your Just collect your data and convert to json and use it in your data field: : Follow the steps from here

Upvotes: 1

Umesh Markande
Umesh Markande

Reputation: 309

You can use ajax success method to get ajax succes response and also use error method whether ajax has error it will catch in error method for example.

$.ajax({
      method:"POST",
            url:"main_class.php",
          data:$('form').serialize() + "&submit=submit",
          success:function(data) {
            Console.log(data)
          }, error: function (e){}
       });

Upvotes: 1

Related Questions