In my laravel app, I have a div on a blade to display total number of daily orders.
<div id="shopify_row1">
<div id="shopify_widget1">
<div >
<img src="https://cdn0.iconfinder.com/data/icons/social-media-2092/100/social-35-512.png" width="32" height="32">
<h6 >Shopify</h6>
<hr >
<h5 >Total Orders</h5>
<span id="tot_o">{{ $tot_o }}</span>
</div>
</div>
</div>
I get this $tot_o
via controller.
Following is my index()
in the controller
if($request->has('selected_date')){
$selected_date=$request->selected_date;
$url = "https://[email protected]/admin/api/2022-10/orders/count.json?status=any&created_at_max=".$selected_date."";
}else{
$selected_date = date('Y-m-d');
$url = "https://[email protected]/admin/api/2022-10/orders/count.json?status=any&created_at_min=".$selected_date."";
}
$curl = curl_init( $url );
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($curl);
curl_close($curl);
$result_1 = json_decode($json_response, TRUE);
$tot_o = $result_1['count'];
return view('dashboard.index', ['sum' => $sum,
'tot_o' => $tot_o]);
Now I'm trying to implement a date picker, so does the value of $tot_o should be changed according to the picked date, on change.
This is my date picker.
<td>
<input id="date" type="date">
</td>
And this is my JavaScript.
<script>
$(document).on('change', '#date', function (e) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: 'GET',
url : '/home',
data : {selected_date : $('#date').val()},
success:function(data){
$('#tot_o').empty();
var total_orders = JSON.parse("{{ json_encode($tot_o) }}")
console.log(total_orders);
},
timeout:10000
});
});
</script>
But here when I console.log my output it always gives me 0, even though the total is greater than the 0...
How can I correct my JS to display the value inside the <span id="tot_o"></span>
correctly...
CodePudding user response:
In the controller AJAX
return response()->json([
'tot_o' => $tot_o
]);
So in AJAX
success:function(data){
var total_orders = data.tot_o;
$('#tot_o').text(total_orders);
},
Span should be (id="tot_o"
)
<span id="tot_o"></span>
You can use .text()
or .html()
to write data
CodePudding user response:
In the controller
if($request->ajax()) { return response(['total_order'=> $tot_o],200); }
in ajax success:
$('#tot_o').text(ordertotal_orders);
in span add id tot_o
<span id="tot_o"></span>