douji9184
douji9184
2018-07-24 13:50

Laravel使用ajax插入数据库的多个复选框

  • laravel
  • php
  • ajax
  • javascript
  • jquery

I need someone to help me please to insert multiple products to cart, adding single one is working fine but I have one product which inserting with no issue and related products which have checkboxes where I need to add it but dont know how

enter image description here

ajax for adding product:

<script>
$(function(){ 
  $('.add_to_cart').on("click", function () { 
    var id = ($("#item_id").val());
    $.ajax({ 
      url: 'cart/' + id+'/edit', 
      type: "get", 
      success: function (data) { 

        $('#myModal').modal('hide'); 
        $('#cart_product').html(data); 
      } 
    }); 
  }); 
});
</script>

add in controller

public function edit(Request $request,$id)
{ 
  $quantity=$request->quantity;

  $product=Product::find($id);
  Cart::add($id,$product->product_title,$quantity,$product->product_price);
  $products = Cart::content();
  foreach($products as $Product){
    echo '<div class="OrderItem_root styles_base styles_spacing-base">
    <div class="OrderItem_quantity styles_just-right styles_base styles_spacing-base">'.$Product->qty.'</div>
    <div class="OrderItem_container">
    <div class="OrderItem_category"></div>
    <div class="OrderItem_itemHeader">
    <div id="cartprice" class="OrderItem_total">$'.$Product->price*$Product->qty.'</div>
    <input id="mycartprice" type="text" name="mycartprice" value="'.$Product->price.'"  hidden="">
    </div>
    <div>
    </div>
    <div>
    <button class="remove_item OrderItem_action Button_root" data-id="'.$Product->rowId.'" data-price="'.$Product->price*$Product->qty.'" data-qty="'.$Product->qty.'" type="submit">Remove</button>
    </div>
    </div>
    </div>';
  }
}

related items

foreach($products as $Product){ 

   echo '<div class="SuggestedItem_container">  
    <label>
    <input  id="ppleadd" type="checkbox" onchange = "AutoCalculateMandateOnChange(this)"> 

    <span id="related_item" class="SuggestedItem_name">'.$ProductDetails->product_title.'</span><span class="SuggestedItem_price styles_small styles_base styles_spacing-base">+$'.$ProductDetails->product_price.'</span></div></div> 

    </div> 
    </label>'; 
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

1条回答

为你推荐