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
4 Answers
Reset to default 3Get 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