I'm working on updating the field $medic->current
based on certain logic that I have in mind.
I'm quite new to using AJAX and would appreciate a little guidance to kick things off.
In my code, I am calculating the difference between two dates { $medic->end_day , current date }. Depending on the difference in dates, I aim to modify the "current" field accordingly. If the difference is less than 0, then set current = 0, else set current = 1. This is what I am trying to achieve.
So, how can I utilize AJAX to update the "current" field of $medic
.
Below you will find the snippet of code:
<script>
window.onload = checkcurrent;
var today = new Date();
@foreach($medics as $medic)
@if($medic->current == 1)
function checkcurrent()
{
var status = {{$medic->current}};
var last_date= new Date('{{ \Carbon\Carbon::createFromFormat('d/m/Y', $medic->end_day)->toDateString() }}');
var new_last_date = new Date(last_date.getFullYear(),last_date.getMonth(),last_date.getDate());
var diff = parseInt((new_last_date - today) / (1000 * 60 * 60 * 24));
if(diff<0)
{
status = 0;
$.ajax({
url: "stocks/{stock_tag}/medications/updatecurrent/{info_id}",
type: "put",
data: { current: 0 } ,
success: function (response) {
console.log('success');
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
}
else
{
status = 1;
}
}
@endif
@endforeach
</script>
Additionally, here is the method in my controller:
public function updatecurrent($stock_tag,$info_id)
{
$stock = Stock::find($stock_tag);
$findInformation= Medication::where('id','=',$info_id)->first();
$id=Input::get('current');
$stock->under_medication=$id;
$findInformation->current=$id;
$stock->save();
$findInformation->save();
}
I specifically need some assistance with refining the ajax code as everything else seems to be functioning correctly. When the status is 0, my goal is to update the "current" field to 0. Any suggestions on how to accomplish this?