php - 關於 php,如何用ajax請求顯示 Flash 消息?

  显示原文与译文双语对照的内容
91 3

在summernote中添加文本時,它保存在資料庫中,然後重新載入頁面。我想在頁面刷新"發送成功"后在頂部顯示消息。如何使用ajax請求添加 Flash 消息?我已經發布了視圖ajax和控制器代碼。如何查看查看信息?

視圖:

 @if (Session::has('error'))
 <div class="alert alert-danger">
 {{ Session::get('error') }}
 </div>
@endif
@if (Session::has('success'))
 <div class="alert alert-success">
 {{ Session::get('success') }}
 </div>
@endif
<div class="row">
 <div class="col-xl-12 col-md-12 col-sm-12">
 <div class="card">
 <div class="card-header">
 <h4 class="card-title">Contact Driver</h4>
 @if($successmessage = Session::get('allsuccess'))
 <div class="alert alert-success alert-block" style="width:300px;">
 <button type="button" class="close" data-dismiss="alert">×</button>
 <strong>{{ $successmessage }} </strong>
 </div>
 @endif
 </div>

Ajax:

 $.ajax({
 url:"{{url('/add_contact_driver')}}",
 type: 'POST',
 data: formData,
 dataType: 'json',
 contentType: false,
 processData: false,
 success: function (response) {
 console.log('response', response)
 window.location.reload();
 }, error: function (error) {
 console.log('create article error', error);
 }
 });

控制器:

 public function contactDriver(Request $request)
{
 $opn = $request->input('search_opn');
 $name = $request->input('search_name');
 $city = $request->input('search_city');
 $editor = $request->input('editor');
 $validation = Validator::make($request->all(), [
 'select_reason' => 'required'
 ]);
 if ($validation->fails()) {
 $response = (new ApiMessageController())->validatemessage($validation->errors()->first());
 } else {
 $contact = new ContactDriver();
 $contact->opn = $opn;
 $contact->name = $name;
 $contact->city = $city;
 $contact->text = $editor;
 $saveContact = $contact->save();
 if ($saveContact) {
 $response = (new ApiMessageController())->saveresponse("Send Successfully");
 } else {
 $response = (new ApiMessageController())->failedresponse(" Failed to send");
 }
 }
 return $response;
}
时间:原作者:0个回答

77 2

你可以這樣做

 success: function (response) {
 console.log('response', response);
 $(".alert-success").css("display","block");
 $(".alert-success").append("<P>This is a message");
 }
<div class="alert alert-success" style="display:none">
 {{ Session::get('success') }}
</div>
原作者:
68 3

你可以在控制器中像這樣返回錯誤/成功消息

return response()->json(['status'=>'error','message'=>'Error Occured']);

或者

return response()->json(['status'=>'error','message'=>'Succesfully Send']);

現在在你的ajax代碼中

在ajax調用中追加錯誤消息框。

 $.ajax({
 url:"{{url('/add_contact_driver')}}",
 type: 'POST',
 data: formData,
 dataType: 'json',
 contentType: false,
 processData: false,
 success: function(result) {
 console.log(result);
 $('#message').html('');
 if(result.status=='success'){
 $('#message').append(
 '<div class="alert alert-success alert-dismissable">'+
 '<button type="button" class="close" data-dismiss="alert">'+
 '<span aria-hidden="true">&times;</span>'+
 '<span class="sr-only">Close</span>'+
 '</button>'+
 result.message+
 '</div>'
 );
 }else{
 $('#message').append(
 '<div class="alert alert-danger alert-dismissable">'+
 '<button type="button" class="close" data-dismiss="alert">'+
 '<span aria-hidden="true">&times;</span>'+
 '<span class="sr-only">Close</span>'+
 '</button>'+
 result.message+
 '</div>'
 );
 }
 },
 error: function(result){
 console.log(result);
 alert('Something went wrong');
 }
 });

在要顯示錯誤的位置添加以下 html,

<div id="message"></div>

我希望你能理解。

原作者:
58 0

你可以做的是

 <div class="alert alert-danger" id="error-div" {{ Session::has('error')? style="display:block" : style="display:none" }}>
 {{ Session::get('error') }}
 </div>
 <div class="alert alert-success" id="success-div" {{ Session::has('success')? style="display:block" : style="display:none" }}>
 {{ Session::get('success') }}
 </div>

將響應設置為

 success: function (response) {
 console.log('response', response)
 $('#success-div').show();
 $('#success-div').html(response);
 }, error: function (error) {
 console.log('create article error', error);
 $('#error-div').show();
 $('#error-div').html
 }
原作者:
110 4

有 2種方法可以做到這一點。

1 ) 如果要重新載入頁面,然後從控制器本身設置成功消息,如下所示:

Session::flash('success', 'This is a message!'); 

然後在視圖文件中訪問這裡成功消息,如下所示:

2 ) 通過從控制器中返回消息,並通過 Jquery ( 在Ajax的成功功能中) 顯示它。

$.ajax({
 url:"{{url('/add_contact_driver')}}",
 type: 'POST',
 data: formData,
 dataType: 'json',
 contentType: false,
 processData: false,
 success: function (response) {
 console.log('response', response)
//window.location.reload();
 $("#YOUR MESSAGE DIV").html(response.message);//Assuming the response object contains the Variable as message.
 }, error: function (error) {
 console.log('create article error', error);
 }
});
原作者:
...