Send multiple data with ajax in PHP


Data can be sent through JSON or via normal POST. Following is an example showing data sent through JSON −

var value_1 = 1;
var value_2 = 2;
var value_3 = 3;
$.ajax({
   type: "POST",
   contentType: "application/json; charset=utf-8",
   url: "your_url_goes_here",
   data: { data_1: value_1, data_2: value_2, data_3: value_3 },
   success: function (result) {
      // perform operations here
   }
});

With normal post, the below code can be used −

$.ajax({
   type: "POST",
   url: $('form').attr("action"),
   data: $('#form0').serialize(),
   success: function (result) {
      // perform operations here
   }
});

An alternate to the above code has been demonstrated below −

data:'id='+ID & 'user_id=' + USERID,
with:
data: {id:ID, user_id:USERID}
so your code will look like this :
$(document).ready(function(){
   $(document).on('click','.show_more',function(){
      var ID = 10;
      var USERID =1;
      $('.show_more').hide();
      $('.loding').show();
      $.ajax({
         type:'POST',
         url:'/ajaxload.php',
         data: {id:ID, user_id:USERID},
         success:function(html){
            $('#show_more_main'+ID).remove();
            $('.post_list').append(html);
         }
      });
   });
});

Updated on: 06-Apr-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements