1

I am trying to get a modal to show upon redirect to a page.

I am following this response (Laravel how to redirect back to boostrap modal dialog window)

My controller:

return redirect('postings')->with('welcome_msg');

My view:

@if(Session::has('welcome_msg'))        
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
@endif

<div class="modal fade" id="myModal role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    ...
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  </div>
</div>

However, the modal is not showing upon pop-up. What am I doing wrong?

jlim
  • 91
  • 4
  • 11

2 Answers2

1
<div class="modal fade" id="myModal role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
  <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    ...
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
  </div>
</div>

@if(Session::has('welcome_msg'))        
<script>
$(function() {
$('#myModal').modal('show');
});
</script>
@endif

This may fix it. HTML has to load before javascript.

PhoenixRebirthed
  • 462
  • 6
  • 15
  • Unfortunately it's still not showing. I have updated my code: return redirect('postings')->with('welcome_msg', 'hello'); to test for the session, and view to include
    {{ Session::get('welcome_msg') }}
    before the modal
    – jlim Nov 16 '17 at 23:01
  • @jlim try – PhoenixRebirthed Nov 16 '17 at 23:12
  • @jlim Did you get this working? – Hashim Aziz Jan 17 '22 at 19:00
0

try this in your blade just before closing tag of </ body>

@if (session()->has('welcome_msg'))
    <script>
        $('#YourModalName').modal('toggle');
    </script>
@endif
Khalid Khan
  • 3,017
  • 1
  • 11
  • 27
kylie
  • 5
  • 5