I want to show the price of an item based on the size a user selects. am using ajax to achieve this, but when I run the request it responds with a status of 500 (Internal Server Error) error in the console and also in the browser it skips the success function and runs the error function thus displaying an alert error.i haven't understood where have gone wrong with my code.have a look
here is my blade file which shows the size and prices
<div class="ps-product__meta">
<span id="getattrprice" class="ps-product__price sale">{{ $products->merch_price }</span>
<span class="ps-product__del">{{ $products->merch_splprice }}</span>
<div class="ps-product__group">
<h6>Size</h6>
<div class="input-group mb-3">
<select class="custom-select" product-id="{{ $products['id'] }}"
id="getprice" name="productsize">
<option value="">Select Size</option>
@foreach ($products->merchadiseattributes as $attribute )
<option value="{{ $attribute->productattr_size }}">{{ $attribute-
>productattr_size }}
</option>
@endforeach
</select>
</div>
the jquery script
$.ajaxSetup({
headers:{
'X-CSRF-TOKEN':$('meta[name="csrf-token"]').attr('content')
}
});
$(document).ready(function(){
$('#getprice').change(function(){
var productsize=$(this).val();
if(productsize==""){
alert("please Select size");
return false;
}
var product_id=$(this).attr("product-id");
// alert(product_id);
$.ajax({
url:'/getproductprice',
data:{productsize:productsize,product_id:product_id},
type:'POST',
success:function(resp){
// alert(resp);
$("#getattrprice").html(+resp);
},error:function(){
alert("Error");
}
});
});
});
the getproductprice method in the controller
public function getproductprice(Request $request)
{
if($request->ajax()){
$data=$request->all();
// echo "<pre>";print_r($data);die;
$getproductprice=Productattribute::where(['product_id'=>$data['product_id'],
'productattr_size'=>$data['productattr_size']])
->first();
return $getproductprice->productattr_price;
}
}
**the route**
Route::post('/getproductprice', [Home_controller::class,'getproductprice'])->name('getproductprice');