有人可以帮我解决这个问题。我有一个控制器,它首先检查记录是否存在。如果记录存在,它应该返回页面并抛出模态消息或js警报。我在使用JQuery时遇到了麻烦,因为我对此并不熟悉。这是我下面的代码;
Public function postInsert_enroll(Request $r)
{
$area_code = Input::get ( 'area_code' );
$sy = Input::get ( 'sy' );
$semester = Input::get ( 'semester' );
$college = Input::get ( 'college' );
$tot_enroll = Input::get ( 'tot_enroll' );
$gender = Input::get ( 'gender' );
$remarks = Input::get ( 'remarks' );
if (Enroll::where('area_code', '=', $area_code)
->where('sy', '=', $sy)
->where('semester', '=', $semester)
->where('gender', '=', $gender)
->where('college', '=', $college)
->exists())
{
return response()->json(['errors'=>errors()]);
}else
{
$enrollments = new Enroll();
$enrollments->area_code = $r->area_code;
$enrollments->sy = $r->sy;
$enrollments->semester = $r->semester;
$enrollments->college = $r->college;
$enrollments->tot_enroll = $r->tot_enroll;
$enrollments->gender = $r->gender;
$enrollments->remarks = $r->remarks;
$enrollments->r_status = 'A';
$enrollments->save();
return response()->json($enrollments);
}
}
在我的情况下,如果记录不存在,它会继续回到我的JQuery并保存记录,并附加模态消息,但是当记录存在时,记录没有保存但没有任何反应,没有模态消息弹出说记录已经存在。
这是我的JQuery:
$.ajax({
url: '/postInsert_enroll',
type: 'post',
data: {
'_token': $('input[name=_token]').val(),
'area_code': $('select[name=area_code]').val(),
'sy': $('select[name=sy]').val(),
'semester': $('select[name=semester]').val(),
'college': $('select[name=college]').val(),
'tot_enroll': $('input[name=tot_enroll]').val(),
'gender': $('select[name=gender]').val(),
'remarks': $('input[name=remarks]').val()
},
success: function(data) {
if (data.errors) {
$('.error').removeClass('hidden');
$('.error').text(data.errors.area_code);
$('.error').text(data.errors.sy);
$('.error').text(data.errors.semester);
$('.error').text(data.errors.college);
$('.error').text(data.errors.tot_enroll);
$('.error').text(data.errors.gender);
$('.error').text(data.errors.remarks);
$('#messcontent').text('Error. Record has not been saved.');
$('#messModal').modal('show');
} else {
$('.error').remove();
$('#enrolltable').append("<tr class='items" + data.id + "' id='" + data.id + "'><td><input type='checkbox' id='chkrows' name='chkrows' class='checkitem' value='" + data.id + "'></td><td>" + data.id + "</td><td>" + data.area_code + "</td><td>" + data.sy + "</td><td>" + data.semester + "</td><td>" + data.college + "</td><td>" + data.gender + "</td><td>" + data.tot_enroll + "</td><td class='enroll-buttons'><a href='#' class='show_enroll btn btn-space' onclick='enroll_show();'><span class='glyphicon glyphicon-eye-open'></span> Show </a><a href='#' class='edit_enroll btn btn-space' onclick='enroll_edit();'><span class='glyphicon glyphicon-edit'></span> Edit </a><a href='#' class='delete-enroll btn btn-space' onclick='enroll_delete();'><span class='glyphicon glyphicon-trash'></span> Delete</button></td></tr>");
$('#messModal').delay(1000).fadeOut(450);
$('#messcontent').text('New enrollment record saved.');
$('#messModal').modal('show');
setTimeout(function(){$('#messModal').modal('hide');}, 1500);
}
},
});