Send multiple <select> elements with jQuery POST
11,610
Solution 1
First of all you have to use classes for your selects instead of an id. jQuery will only return one element when you use an id. After that the following function will convert all values of the selects you give as paramater as an array.
/**
* Convert select to array with values
*/
function serealizeSelects (select)
{
var array = [];
select.each(function(){ array.push($(this).val()) });
return array;
}
So:
var course_ids = serealizeSelects($('.course_id'));
Should for example return:
[1,3,2]
Solution 2
I would give every select
element a unique name, an array would also do well here, see this example:
$('#selected > tbody:last').append('<tr><td><select id=\'plan_id\' name="my_select[]"><option value=\'1\'>One</option><option value=\'2\'>Two</option><option value=\'3\'>Three</option></select></td></tr>');
and then just serialize the form before you send it in:
var my_data = $("form").serialize();
...
Edit: The complete example:
$("#add").click(function() {
$('#selected > tbody:last').append('<tr><td><select id=\'plan_id\' name="my_select[]"><option value=\'1\'>One</option><option value=\'2\'>Two</option><option value=\'3\'>Three</option></select></td></tr>');
});
^^^^^^^^^^^^^^^^^^^
and:
$('#course_update').click(function() {
var my_data = $("form").serialize();
$('#update_status').html('<img src="../images/ajax-loader.gif" />');
$.post('../update.php', my_data, function(data) {
$('#update_status').html(data);
return false;
});
});
Author by
David
Updated on June 05, 2022Comments
-
David almost 2 years
I have this jQuery code to add
<select>
elements in a table row with an "add" button:$("#add").click(function() { $('#selected > tbody:last').append('<tr><td><select id=\'plan_id\'><option value=\'1\'>One</option><option value=\'2\'>Two</option><option value=\'3\'>Three</option></select></td></tr>'); });
What do I need to modify in the below code to be able to POST multiple values from the select elements as an array? Right now it only inserts one value at the time to my MySQL database.
$('#course_update').click(function() { var course_id = $('#course_id').val(); var plan_id = $('#plan_id').val(); var price_id = $('#price_id').val(); var course_name = $('#course_name').val(); var course_isActive = $('#course_isActive').val(); var course_city_id = $('#course_city_id').val(); $('#update_status').html('<img src="../images/ajax-loader.gif" />'); $.post('../update.php', { course_id: course_id, plan_id : plan_id, price_id: price_id, course_name: course_name, course_city_id: course_city_id, course_isActive: course_isActive }, function(data) { $('#update_status').html(data); return false; }); });
-
David almost 12 yearsThank you. So, after I have created the variable for
plan_ids
(you called it course_ids) how do I send this with jQuery POST? Please describe using my code in the original question! -
David almost 12 yearsI follow you, however I am unable to make it work inside my code. Would you mind providing me with your code, inside mine as posted in the original question? Thank's in advance.
-
Lumocra almost 12 yearsLike this:
var plan_ids = serealizeSelects($('.plan_id')); $.post('../update.php', { plan_ids: plan_ids }, ...
-
David almost 12 yearsThank you, It work's now! Now I only need to figure out how to collect the values inside my update.php
-
Lumocra almost 12 yearsIn case of the example above, in your PHP
$_POST['course_ids']
will hold:array(1,3,2)