通过发布数据jQuery

I can' figure how to do that.

I have several hidden fields

<input type="hidden" id="order_1" name="order_1" value="1">
<input type="hidden" id="order_2" name="order_2" value="2">
<input type="hidden" id="order_3" name="order_3" value="3">

<input type="hidden" name="ids[]" value="1">
<input type="hidden" name="ids[]" value="2">
<input type="hidden" name="ids[]" value="3">

etc.. The idea is to pass the data via ajax like this.

var id = $('input').attr('name')
$.post('http://foobar.com/ajax.php', {ids : ids, order: order}, function(data){

});

What I can't figure out is how to collect the data.

On the other side in ajax.php I wan't to get the values like this

echo $_POST['order_1'];
echo $_POST['order_2'];
......
etc

Mistake #1

<input type="hidden" id="order_1" name="order_1 value="1">
<input type="hidden" id="order_1" name="order_1 value="1">
<input type="hidden" id="order_1" name="order_1 value="1">

should be

<input type="hidden" id="order_1" name="order_1" value="1">
<input type="hidden" id="order_1" name="order_1" value="1">
<input type="hidden" id="order_1" name="order_1" value="1">

Mistake #2

Never ever forever use same id. Use different one ! Also for name attribute


Mistake #3

You pass

$.post('http://foobar.com/ajax.php', {ids : ids

instead of

$.post('http://foobar.com/ajax.php', {ids : id


update

Assuming

<input type="hidden" id="order_1" name="order_1" value="1">
<input type="hidden" id="order_2" name="order_2" value="2">
<input type="hidden" id="order_3" name="order_3" value="3">

and js: var yourArray = [];

$.each($("input[type='hidden']"), function(key, value){
  yourArray.push($(this).attr("name"));
});

pass it to ajax

$.post('http://foobar.com/ajax.php', {ids : yourArray ...

I think your looking for .serialize. See the api.

Try something like this:

var postData = $('input:hidden').serialize();

$.post('http://foobar.com/ajax.php', postData, function(data){

});

Bear in mind the selector I used for the hidden inputs can be changed by you to be more specific, perhaps by using the form id.

This will post all your form data to ajax.php. You'll have to give your <form> tag an id (in this case its id='formId')

$.post('http://foobar.com/ajax.php', $('#formId').serializeArray());

You can request the ids with:

$ids = $_POST['ids'];

To pass the values

<input type="hidden" id="order_1" name="order_1" value="1">
<input type="hidden" id="order_2" name="order_2" value="2">
<input type="hidden" id="order_3" name="order_3" value="3">

<input type="hidden" name="ids[]" value="1">
<input type="hidden" name="ids[]" value="2">
<input type="hidden" name="ids[]" value="3">


var ids_arr = new Array();
 $("input[name='ids[]']").each(function() {
    ids_arr.push( $(this).val() );
 });
$.post('http://foobar.com/ajax.php', {"ids" :ids_arr , "order_1": $('#order_1').val(),"order_2":$('#order_2').val(),"order_3":$('#order_3').val()}, function(data){

});

For getting the values on php side

echo $_POST['order_1'];
echo $_POST['order_2'];
echo $_POST['order_3'];
var_dump($_POST['ids']);