Reputation: 13
I do post operation with jquery. I have multiple checkbox boxes as below. I want to post a multidimensional array, but my operation fails. Where am I doing wrong when I think there is a javascript error?
<input type="checkbox" name="options[0][menu][1]" checked="" autocomplete="off" value="Mantar">
<input type="checkbox" name="options[1][menu][1]" checked="" autocomplete="off" value="Soğan">
<input type="checkbox" name="options[3][menu][1]" checked="" autocomplete="off" value="Mısır">
input structure
<?php foreach ($gercek_dizi as $key => $value) { ?>
<input class="form-control menu" type="hidden" id="menu[<?php echo $keys[1]; ?>]" name="menu[<?php echo $keys[1]; ?>]" value="Ürün <?php echo $keys[1]; ?>" >
<?php $a=0; foreach ($value as $ky => $cek) { $a++;?>
<li class="col-md-2 list-group-item mr-60">
<div class="btn-group-toggle pd-5" data-toggle="buttons">
<label class="btn btn-secondary btn-xs br-30 cizgicek active"><input type="checkbox" name="options[<?php echo $ky; ?>][menu][<?php echo $keys[1]; ?>]" checked autocomplete="off" value="<?php echo $cek['menu']; ?>"/> <?php echo $cek['menu']; ?> </label>
</div>
</li>
<?php } ?>
<?php } ?>
As I have given above, I keep the array in the name. I want to post this, but the options[]
field is just being posted. name = "options[0][menu][1]"
How can I post this section completely?
$('.add_cart').click(function(){
var csrfName = $('.txt_csrfname').attr('name');
var csrfHash = $('.txt_csrfname').val();
var product_id = $(this).data("productid");
var product_name = $(this).data("productname");
var product_price = $(this).data("price");
var quantity = $('#' + product_id).val();
var image = $(this).data("image");
var options = [];
var resimli = [];
var coklu = [];
var menu = [];
$(':checkbox:checked').each(function(i){
options[i] = $(this).val();
});
$('input.menu').each(function(i){
menu[i] = $(this).val();
});
$('select option:selected').each(function(i){
coklu[i] = $(this).val();
});
$(':radio:checked').each(function(i){
resimli[i] = $(this).val();
});
if(quantity != '' && quantity > 0)
{
$.ajax({
url:"<?php echo base_url(); ?>cart_controller/add",
method:"POST",
data:{product_id:product_id, product_name:product_name, product_price:product_price, quantity:quantity, menu:menu, options:options, coklu:coklu, resimli:resimli, image:image, [csrfName]:csrfHash},
dataType: 'json',
success:function(response)
{
$('.txt_csrfname').val(response.token);
iziToast.success({
title: 'Sepete Eklendi',
message: 'Ürün Sepetinize Eklendi!',
position: 'topRight',
});
$('.sepetim').load("<?php echo base_url(); ?>cart_controller/sepet");
$('.sepetsay').load("<?php echo base_url(); ?>cart_controller/sepetsay");
}
});
}
else
{
iziToast.error({
title: 'Dikkat!',
message: 'Lütfen Ürün Adetini Seçin!',
position: 'topRight',
})
}
});
Result Options Name;
Array ( [0] => Mantar [1] => Zeytin [2] => Domates [3] => Mısır [4] => Turşu )
The result I want;
Array ( [1] => Array ( [menu] => Array ( [1] => Mantar ) ) [2] => Array ( [menu] => Array ( [1] => Zeytin ) ) [3] => Array ( [menu] => Array ( [1] => Domates ) ) [4] => Array ( [menu] => Array ( [2] => Mısır ) ) [5] => Array ( [menu] => Array ( [2] => Turşu )
Upvotes: 0
Views: 78
Reputation: 13
@Wawam edits
$(':checkbox:checked').each(function(i){
options.push({menu: { 1: $(this).val() }});
});
add attr input
data-menuid="2"
$(':checkbox:checked').each(function(i){
options.push({menu: { [$(this).data("menuid")]: $(this).val() }});
});
Upvotes: 1
Reputation: 497
when you do :
options[i] = $(this).val();
you lose the multidimentional's structure.
You can try something like that :
$(':checkbox:checked').each(function(i){
options.push({menu: { 1: $(this).val() }});
});
Upvotes: 0