我有一个表格,我将数据插入到dsatabase usind laravel请求中。 它工作正常,但我想使用ahax将其插入数据库而不刷新页面。我不知道如何去做。表格在下面
<form method="POST" action="{{url('Others/Doctor/Diagnosis/patient_diagnosis')}}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="appointment_id" value="{{ $apt->id }}">
<input type="hidden" name="doctor_id" value="{{ $apt->doctor_id }}">
<input type="hidden" name="patient_id" value="{{ $apt->patient_id }}">
<div class="row">
<div class="form-group {{ $errors->has('complaints') ? ' has-error' : '' }}">
<label for="lastname" class="col-md-3 control-label">Complaints</label>
{{--<input type="text" name="complaints" id="complaints" class="form-control">--}}
<textarea name="complaints" id="complaints" class="form-control" rows="2" cols="5"></textarea>
@if ($errors->has('complaints'))
<span class="help-block">
<strong>{{ $errors->first('complaints') }}</strong>
</span>
@endif
</div>
<div class="form-group pull-right">
<button type="submit" id="button" class="btn btn-primary">Add Case Study</button>
</div>
</div>
</form>
然后我的控制器如下
public function postCaseStudy(Request $request){
//$request=Input::all();
//dd($request);
$this->validate($request,[
'complaints' => 'required'
]);
$appointment_id = $request[trim('appointment_id')];
$patient_id = $request[trim('patient_id')];
$doctor_id = $request[trim('doctor_id')];
$complaints = $request[trim('complaints')];
//dd($patient_id);
$PatientCase = new PatientCase();
$PatientCase->appointment_id = $appointment_id;
$PatientCase->patient_id = $patient_id;
$PatientCase->doctor_id = $doctor_id;
$PatientCase->complaints = $complaints;
$PatientCase->save();
return Redirect::to('Others/Doctor/Diagnosis/patient_diagnosis/'.$patient_id);
}
它有效,但我想使用ajax,因此页面不会刷新
答案 0 :(得分:0)
给出一个id来形成例如id =“form”。在脚本中添加jquery
<script>
$(function(){
$("#button").click(function(event){
event.preventDefault();
$.ajax({
url: $('#form').attr('action');,
type:'POST',
data:$('#form').serialize(),
success:function(result){
//Do something with return data
}
});
});
});
</script>