php - jquery : pass an array in ajax - Stack Overflow

I have a form with several identical fields:<input type="text" id="qte" value=&q

I have a form with several identical fields:

<input type="text" id="qte" value="" name="qte[]">

How transmetre the array in my file processing?

I noticed that the array sent ajax became a string.

$("#form_mande").submit(function(event){

var qte = $("#qte").val();

if(qte== '')
{
    $('#qte_message').html("KO QTE");
}
else
{
    $.ajax({
        type : "POST",
        url: $(this).attr('action'),
        data: $(this).serialize(),
        success : function(){
            $('#form_mande').html('<p>OK</p>');
        },
        error: function(){
            $('#form_mande').html("<p>KO</p>");
        }
    });
}
return false;

}

I have a form with several identical fields:

<input type="text" id="qte" value="" name="qte[]">

How transmetre the array in my file processing?

I noticed that the array sent ajax became a string.

$("#form_mande").submit(function(event){

var qte = $("#qte").val();

if(qte== '')
{
    $('#qte_message').html("KO QTE");
}
else
{
    $.ajax({
        type : "POST",
        url: $(this).attr('action'),
        data: $(this).serialize(),
        success : function(){
            $('#form_mande').html('<p>OK</p>');
        },
        error: function(){
            $('#form_mande').html("<p>KO</p>");
        }
    });
}
return false;

}

Share Improve this question asked Sep 2, 2013 at 14:44 Christophe MartinChristophe Martin 2231 gold badge4 silver badges11 bronze badges 5
  • 2 possible duplicate of Serializing to JSON in jQuery – Joren Commented Sep 2, 2013 at 14:46
  • If it's sending a string then you probably need to use serializeArray(). – Ben Fortune Commented Sep 2, 2013 at 14:47
  • You can't have several fields with the same ID jQuery will only ever recognize the first one it sees. ID's are unique identifiers. – Rick Calder Commented Sep 2, 2013 at 14:48
  • The array bees a JSON string. – Jack M. Commented Sep 2, 2013 at 14:51
  • You cant have identical fields with same id, may be you can use class – SarathSprakash Commented Sep 2, 2013 at 14:52
Add a ment  | 

4 Answers 4

Reset to default 3

Get value in jquery like:

$("#form_mande").submit(function(event){

    var qte_array = new Array();
    $('input[name="qte[]"]').each(function(){
       qte_array.push($(this).val());
    });

    if(qte_array.length== 0)
    {
        $('#qte_message').html("KO QTE");
    } 
    else
    {
        $.ajax({
            type : "POST",
            url: $(this).attr('action'),
            data: {qte:qte_array},
            success : function(){
               $('#form_mande').html('<p>OK</p>');
            },
            error: function(){
                $('#form_mande').html("<p>KO</p>");
            }
        });
    }
});

and get it in php like:

$qte = $_POST["qte"];

here qte an array

This returns the input textbox object:

$("#qte");

This returns the value of the input textbox object:

$("#qte").val();

Remember you asked for DOM object by id, and this by definition returns only one.

Please read this topic: JQuery - Reading an array of form values and displaying it?

In short you should iterate with tag name="qte[]" instead of using ids. In DOM you cannot have two different objects with different ids.

var qte_array = new Array();
$('input[name="qte[]"]').each(function(){
   qte_array.push($(this).val());
});

After this you have all the values of qte[] array in one object - qte_array. You can later serialize this array to JSON and then pass it as a string.

ALTHOUGH - you shouldn't need to do all those things. You can send ajax request directly with your form, all those data in these inputs will be transferred anyway. You just need to handle them correctly server-side.

id is unique, you can't have more fields with the same ID.

By the way you should convert values to JSON and pass them to Ajax

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744350956a4569971.html

相关推荐

  • php - jquery : pass an array in ajax - Stack Overflow

    I have a form with several identical fields:<input type="text" id="qte" value=&q

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信