Undefined variable $totalPrice
getting this error i want print $totalPrice in html code form jQuery. not working code after success status
function applyCouponCode() {
jQuery('#coupon_code_msg').html('');
var coupon_code = jQuery('#coupon_code').val();
if (coupon_code != '') {
jQuery.ajax({
// url: '{{ route('apply_coupon_code') }}',
// method: "POST",
type: 'post',
url: '/apply_coupon_code',
data: 'coupon_code=' + coupon_code + '&_token=' + jQuery("[name='_token']").val(),
success: function(result) {
if (result.status == 'success') {
jQuery('.show_coupon_box').removeClass('hide');
jQuery('#coupon_code_str').html(coupon_code);
jQuery('#total_price').html('INR ' + result.totalPrice);
jQuery('.apply_coupon_code_box').hide();
} else {
}
jQuery('#coupon_code_msg').html(result.msg);
}
});
} else {
jQuery('#coupon_code_msg').html('Please enter coupon code');
}
}
function remove_coupon_code() {
jQuery('#coupon_code_msg').html('');
var coupon_code = jQuery('#coupon_code').val();
jQuery('#coupon_code').val('');
if (coupon_code != '') {
jQuery.ajax({
type: 'post',
url: '/remove_coupon_code',
data: 'coupon_code=' + coupon_code + '&_token=' + jQuery("[name='_token']").val(),
success: function(result) {
if (result.status == 'success') {
jQuery('.show_coupon_box').addClass('hide');
jQuery('#coupon_code_str').html('');
jQuery('#total_price').html('INR ' + result.totalPrice);
jQuery('.apply_coupon_code_box').show();
} else {
}
jQuery('#coupon_code_msg').html(result.msg);
}
});
}
}
this is my html code
<ul>
@php $total = 0 @endphp
@foreach ($cart as $details)
@php $total += $details->product_price * $details->quantity @endphp
@endforeach
<li>Cart Subtotal<span>{{ $total }}</span></li>
<tr class="hide show_coupon_box">
<th>Coupon Code <a href="javascript:void(0)" onclick="remove_coupon_code()"
class="remove_coupon_code_link">Remove</a></th>
<td id="coupon_code_str"></td>
</tr>
<li>Shipping<span>Free</span></li>
{{-- <li class="last" id="total_price">You
Pay<span>{{ $totalPrice }}</span>
</li> --}}
<td id="total_price">INR {{ $totalPrice }}</td>
</ul>
2
Answers
You bring in the TotalPrice value by using jQuery does not mean you can define the TotalPrice in the DIV.
Change the code :
To :
If no other errors, the totalPrice value will be brought in into the total_price id’s DIV and the value will be shown.
This issue is not related to jQuery, you don’t have initialized $totalPrice in your blade file, either send it from your backend in compact or make in your blade file.
One more thing if you are assigning $totalPrice to html like in the pic then you don’t need to do {{$totalPrice}}