3

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_commande").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_commande').html('<p>OK</p>');
        },
        error: function(){
            $('#form_commande').html("<p>KO</p>");
        }
    });
}
return false;

}

Christophe Martin
  • 223
  • 1
  • 4
  • 11

4 Answers4

3

Get value in jquery like:

$("#form_commande").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_commande').html('<p>OK</p>');
            },
            error: function(){
                $('#form_commande').html("<p>KO</p>");
            }
        });
    }
});

and get it in php like:

$qte = $_POST["qte"];

here qte an array

MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
  • I also have a field name, surname and email. How to send the data (data :) and recover $ _post [] in php? Thank you for your help – Christophe Martin Sep 04 '13 at 09:53
  • @ChristopheMartin: Change line as data: {qte:qte_array, surname:"Name", email:"email@domain.com"}, and get it in php $surname = $_POST["surname"]; $email = $_POST["email"]; – MD SHAHIDUL ISLAM Sep 04 '13 at 10:00
  • Excellent; I really like being able to pass an array directly and receive it as an array – veeTrain Oct 11 '13 at 20:51
2

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.

Marek
  • 7,337
  • 1
  • 22
  • 33
2

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.

Community
  • 1
  • 1
Kelu Thatsall
  • 2,494
  • 1
  • 22
  • 50
1

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

Fez Vrasta
  • 14,110
  • 21
  • 98
  • 160