MAHAR MAHAR - 4 months ago 47
jQuery Question

Ajax post multiple data

How to post multiple data using ajax? i want to post input

value
and
attr
both. Here is an example :



$('.input[type=\'text\']').keyup(function(){
$.ajax({
type: 'POST',
url: 'index.php',
data: $('.input[type=\'text\']'),
dataType: 'json',
success: function(json) {
alert('done');
}
});
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" value="" attr="yes" />




Answer

As documented, "data" has mixed type (Type: PlainObject or String or Array). So you can assign array or object to data property.

Try following;

$('.input[type=\'text\']').keyup(function(){       
    var dataToPost = {
                    value: $(this).attr('attr'),
                    attr: $(this).val()
                  }; 
            $.ajax({
                  type: 'POST',
                  url: 'index.php',
                  data: dataToPost,
                  dataType: 'json',
                  success: function(json) {
                              alert('done');
                    }
            });
  });