php - jQuery, ajax multiple checkboxes into $_POST array -
i'm trying send $_post data page add sessions simple shopping cart.
i have multiple forms within php while loop each multiple checkboxes, works apart checkboxes.
my question how change piece of code change "item_extras" array?
if(this.checked) item_extras = $(this).val();
i have tried following but, creates 1 line values instead of row within array. if confusing create sample if helps.
if(this.checked) item_extras += $(this).val();
$('form[id^="add_item_form"]').on('submit', function(){ //alert("on click works"); event.preventdefault(); additem($(this)); }); function additem(ele) { //alert("i'm in additem function"); var item_id = ele.parent().parent().find("input[name=item_id]").val(); // item id var item_name = ele.parent().parent().find("input[name=item_name]").val(); // item name var item_options = ele.parent().parent().find('#options').val(); // selected option var item_extras = ""; $item_extras = ele.parent().parent().find('input[name^="extra"]'); // find extras $item_extras.each(function() { if(this.checked) item_extras = $(this).val(); // how make array??? }); alert("before ajax"); var datastring = 'item_id=' + item_id + '&item_name=' + item_name + '&item_options=' + item_options + '&item_extras[]=' + item_extras; alert(datastring); $.ajax({ type: "post", cache: false, url: "includes/cart.php", data: datastring, success: function () { $.ajax({ url: 'includes/cart.php', success: function(data) { $('#cart').html(data); alert("ajax success"); } }); } }); return false; }
you can use serialize method. form.serialize()
$( "form" ).on( "submit", function( event ) { event.preventdefault(); var data = $(this).serialize(); });
Comments
Post a Comment