I have this jQuery code to add elements in a table row with an \"add\" button:
$(\"#add\").click(function() {
$(\'#selected &g
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;
});
});
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]