0

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>
Don'tDownvoteMe
  • 501
  • 2
  • 16

2 Answers2

1

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){}
       });
Umesh Markande
  • 289
  • 1
  • 6
  • It throws an error. `success` construct is not permitted when using data in the above format. – Don'tDownvoteMe Jul 31 '18 at 05:31
  • You can use form object like this. var form = $('form').serializeArray(); var uniquekey = { // other value you want to put name: "uniquekey", value: $('#UniqueKey').val() }; form.push(uniquekey);. And pass this form to data – Umesh Markande Jul 31 '18 at 05:36
  • Just tried what you said and it throws an error too. Could you plz guide me through the correct way of doing that? – Don'tDownvoteMe Jul 31 '18 at 05:47
1

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

rufatZZ
  • 743
  • 2
  • 11
  • 27