user006779
user006779

Reputation: 1031

Fetch input value from certain class and send with jquery

<input id="u1" class="username">
<input id="u2" class="username">
<input id="u3" class="username">
...

How to fetch input value with "username" class and send with ajax jquery to php page.

i want to recive data like simple array or simple json. (i need INPUT values and not ids)

Upvotes: 0

Views: 131

Answers (3)

Gaurav
Gaurav

Reputation: 28765

var values = new Array();
$('.username').each(function(){
    values.push( $(this).val());
});

Upvotes: 1

jeremysawesome
jeremysawesome

Reputation: 7254

I find it best to use jQuery's built in serialize method. It sends the form data just like a normal for submit would. You simply give jQuery the id of your form and it takes care of the rest. You can even grab the forms action if you would like.

$.ajax({
  url: "test.php",
  type: "POST",
  data: $("#your-form").serialize(),
  success: function(data){
    //alert response from server
    alert(data);
  }
});

Upvotes: 1

Amber
Amber

Reputation: 527173

var inputValues = [];
$('input.username').each(function() { inputValues.push($(this).val()); });

// Do whatever you want with the inputValues array

Upvotes: 2

Related Questions