Home > Net >  How to pass a variable to php page beside this.serialize with AJAX
How to pass a variable to php page beside this.serialize with AJAX

Time:11-26

I need to pass a variable from ajax to php beside the serialize data.

My code will explain what I mean:

        $('#update_form').on('submit', function(e){
            e.preventDefault();
            var total = parseFloat($('#total').text()); // This is the var I want to pass
            if($('.check_box:checked').length > 0)
            {
                $.ajax({
                    url:"pages/Model/multiple_update.php",
                    method:"POST",
                    data:$(this).serialize(),
                    success:function()
                    {
                        alert('Data Updated');
                        $('#multiple_update').attr('disabled', 'disabled');
                        fetch_data();
                    }
                });
            }
        });

CodePudding user response:

serialize() creates a querystring in the format a=1&b=2 so you could add to it like this

var total = parseFloat($('#total').text()); 
var qs = $(this).serialize()   '&total='   total;

and then send that

data:qs,

Or

data:$(this).serialize()   '&total='   total,
  • Related