0

I have one page called modals.blade.php in this I am performing an insert operation. This modal I am using on another page called view.blade.php. And the same modal I am using in other pages with different logic (the fields will be changing)

I need to call a function of view.blade.php from modals.blade.php after successful insertion. whether it is possible?

pages

modals.blade.php

<div class="modal fade" id="paymentModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg" role="document">
      <div class="modal-content">
        <form id="paymentform" method="post">
            {{ csrf_field() }}
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLongTitle">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">
           modal body 
        </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary">Add</button>
            </div>
        </form>
      </div>
    </div>
</div>


<script type="text/javascript">

$(document).ready(function(){

    $('#paymentform').on('submit',function(event){

    event.preventDefault();

    var formdata = $('#paymentform').serialize();
    $.ajax({
        url:'{!! url('storepayment') !!}',
        dataType:'json',
        method:'post',
        data: formdata,
        success:function(data)
        {
            getAllPayments();  //  Calling this function from another page
        }

    });

    });

});

</script>

view.blade.php

@include('invoice.modals')
<div class="row ">
                    <div class="col-lg-12">
                        <table class="table table-bordered" id="contactdata" >
                            <thead>
                                <tr>
                                  <th scope="col">SLNO</th>
                                  <th scope="col">Invoice ID</th>
                                  <th scope="col">Payment Method</th>
                                  <th scope="col">Payment Date</th>
                                  <th scope="col">Amount</th>
                                  <th scope="col">Notes</th>
                                  <th scope="col">edit</th>
                                </tr>
                              </thead>
                        </table>
                </div>
 </div>

<script>

    $(function() {

     getAllPayments();

        function getAllPayments()
        {
            var invoiceid = $('#invoiceid').val();    
            $('#contactdata').DataTable({
            processing: true,
            serverSide: true,
            "bDestroy": true,
            "bAutoWidth": false,
            ajax:{
                "url" : '{!! url('payments') !!}',
                data :{
                    'invoiceid' : invoiceid
                },
            },
            columns: [
                    { data:'DT_RowIndex'},
                    { data: 'DT_RowData.data-invoice_id', name: 'invoice_id' },
                    { data: 'payment_method', name: 'payment_method' },
                    { data: 'payment_date', name: 'payment_date' },
                    { data: 'amount', name: 'amount' },
                    { data: 'notes', name: 'notes' },
                ]
            });
        }
 });

Thank you in advance

1
  • 1
    create separate js file for your getallPayments() method and include it to your blade. Commented Feb 26, 2020 at 7:31

1 Answer 1

1

Create separate js file for your method.

public/assets/js/myFunctions.js

function getAllPayments()
{
  //getting all payments
}

In your main layout add a @stack('scripts')

in your view.blade.php

@push('scripts')
 <script src="{{ asset('js/myFunctions.js')}}"></script>
@endpush

and you can now your method since it is included in your blade.

Sign up to request clarification or add additional context in comments.

1 Comment

it will still work just include also the modal in your blade

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.