First I choose Color button:
<form class="form form-variant">
{{ csrf_field() }}
<button type="submit" class="btn btn-success checkbox_click" id="colorChange" style="colors: purple !important"> Color </button>
</form>
My javascript
<script type="text/javascript">
$(document).ready(function(){
$('#myForm{{$skuItemVariant_colorJs->SkuColorId}}').submit(function(e){
e.preventDefault();
$.ajax({
url: "{{ url('product-detail/getsize') }}",
type: "POST",
data: $('.form-variant{{$skuItemVariant_colorJs->SkuColorId}}').serialize(),
success: function(data){
//alert("Form submission success!");
},
error: function(){
alert("Form submission failed!");
}
});
});
});
Then My Json respons:
{Id: 18, SkuItemId: 19, SkuSizeId: 17, SkuColorId: 18, SkuVariantStock: 1, Barcode: "000018",…},…]
0
:
{Id: 18, SkuItemId: 19, SkuSizeId: 17, SkuColorId: 18, SkuVariantStock: 1, Barcode: "000018",…}
1
:
{Id: 20, SkuItemId: 19, SkuSizeId: 18, SkuColorId: 18, SkuVariantStock: 1, Barcode: "000020",…}
I would like to pass it via Ajax and create a new button base on data availabe of Size SkuItemId. I have this JS Code :
so, if I choose Color then appear Size of data
2
Answers
step 1. Write this code to your main layout in head tags
step 2. use @csrf in your form
step 3. Write this between your ajax codes
you can loop through the array using JavaScript and generate a button for each unique SkuItemId value.
$(document).ready(function(){
var skuItems = {!! json_encode($yourArray) !!}; // assuming $yourArray contains the array you provided
});