Dec 16, 2021
0
Level 5
Laravel get updated data after delete to ajax
i am trying to delete the row and update the div without refresh and i have a code like below,
File Path =>admin/partials/useranswerfile.blade.php
<div id="fileuploaddiv">
@if($checkExists)
<a href="#" class="btn btn-danger btn-xs" onclick="deleteUserAnswerCopy(event,0)"> <i class="fa fa-trash-o"></i> Delete</a>
<form class="deleteuseranswercopy">
{{ csrf_field() }}
<input type="hidden" value="{{ $checkExists->upload_uuid }}" name="upload_uuid" />
<input type="hidden" value="{{ $testdetail->test_code }}" name="testcode" />
<input type="hidden" value="{{ $coursedetails->course_code }}" name="coursecode" />
</form>
@else
<p>File Present <a href="#">Click Here</a>
@endif
</div>
<script>
function deleteUserAnswerCopy(event,isEmail)
{
event.preventDefault();
var formData = $('.deleteuseranswercopy').serialize();
$.ajax({
type: "DELETE",
url: "{{ route('deleteuseranswercopy') }}",
data: formData,
dataType: "json",
}).done(function(data) {
if (data.status == 200) {
$('#fileuploaddiv').empty().append(data.responseFormHtml);
} else {
toastr.error('Something went wrong try again');
return false;
}
}).fail(function(err) {
return false;
}).always(function() {
//do something whether request is ok or fail
});
}
</script>
In Controller,
$checkExists = UploadAnswer::where('test_id',$testdetail->test_id)->whereIn('status',[1,2])->where('upload_uuid',$uploaduuid)->first();
if(!$checkExists)
return json_encode(array('status' => 600, 'info' => 'Answer Copy not found.'));
$checkExists->delete();
$responseFormHtml = View::make('admin.partials.useranswerfile', compact('checkExists'))->render();
return json_encode(array('status' => 200 ,'info' => 'You have Successfully deleted uploaded Answer copy file.','responseFormHtml' => $responseFormHtml));
In above code i am using $checkExists variable to check if file uploaded or not. if it is not uploaded then i am displaying the form to upload and if file uploaded then i am displaying the file name with delete button.
But after delete in controller $checkExists variable is not updating. i have to write a query like below to update the variable.
$checkExists = UploadAnswer::where('test_id',$testdetail->test_id)->whereIn('status',[1,2])->where('upload_uuid',$uploaduuid)->first();
Any alternate way to do this?
Please or to participate in this conversation.