Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nagendrabhat's avatar

App.layout Crashing the Ajax function used in View

App.blade

<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">

<title>{{ config('app.name', 'Healthfix Pharma') }}</title>

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<script>
    window.Laravel = {!! json_encode([
        'csrfToken' => csrf_token(),
    ]) !!};
</script>

                <!-- Collapsed Hamburger -->
                <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse">
                    <span class="sr-only">Toggle Navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <!-- Branding Image -->
                <a class="navbar-brand" href="{{ url('/') }}">
                    {{ config('app.name', 'Healthfix Pharma') }}
                </a>
            </div>

            <div class="collapse navbar-collapse" id="app-navbar-collapse">
                <!-- Left Side Of Navbar -->
                <ul class="nav navbar-nav">
                    &nbsp;
                </ul>

                <!-- Right Side Of Navbar -->
                <ul class="nav navbar-nav navbar-right">
                    <!-- Authentication Links -->
                    @if (Auth::guest())
                        <li><a href="{{ route('login') }}">Login</a></li>
                        <!--li><a href="{{ route('register') }}">Register</a></li-->
                    @else
                        <li class="dropdown">
                            <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
                                {{ Auth::user()->username }} <span class="caret"></span>
                            </a>

                            <ul class="dropdown-menu" role="menu">
                                <li>
                                    <a href="{{ route('logout') }}"
                                        onclick="event.preventDefault();
                                                 document.getElementById('logout-form').submit();">
                                        Logout
                                    </a>

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
                                        {{ csrf_field() }}
                                    </form>
                                </li>
                            </ul>
                        </li>
                    @endif
                </ul>
            </div>
        </div>
    </nav>

    @yield('content')
</div>

<!-- Scripts -->

<script src="{{ asset('js/app.js') }}"></script>

And here is my page added only the code

@extends('layouts.app') @section('content')

<meta name="_token" content="{!! csrf_token() !!}"></meta>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
jQuery.noConflict();
</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

Enter Sampling $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content') } })
//----------------- Clicking on Add Customer ------
jQuery('#add').on('click',function(){
  alert("hi");
  $('#save').val('Save');
  $('#frmRepresentative').trigger('reset');

  $('#Retailer').modal('show');
});
@endsection
0 likes
13 replies
nagendrabhat's avatar

Not able to call the Add customer Ajax function. Its not even calling alert. When i remove the app file. It works fine. Tried with noConflict after the jQuery. But seems to be not working

Nash's avatar

Are you loading jQuery as part of your compiled app.js file (check if your bootstrap.js file contains any references to jQuery)? Either load it there or use a CDN like you are doing now but don't do both at the same time. Also remember to put your own click handler code after jQuery has been loaded.

nagendrabhat's avatar

@Nash, I exactly didn't follow you. Can you please elaborate more on this. Or can you help me out with a code? I believe we dont need to change anything on App. Because its system generated file.

Can you please help me out with the code. You help would really moves my work further .

Nash's avatar

I think you are already loading Bootstrap and jQuery in your app.js file. If so, you don't need to load them in <head>, just put your own code after app.js.

nagendrabhat's avatar

@Snapey, Yeah its a big set of code which i am debugging, so could not explain in single thread. Please review it.

Snapey's avatar

Well the issue has already been explained. app.js that comes with the framework is an assembly of several libraries including jquery.

You don't want to be loading jquery again if you are including app.js

nagendrabhat's avatar

Yes I removed the jQuery and bootstrap links from the page which I have created and extended the app. Still not able to make work on the ajax call created in newpage

nagendrabhat's avatar

Now. I commented on the app.js from app.blade And it works fine for me.

But i just want to make sure where this is gonna impacted in the future development.

Anyone has any information on where is app.js present in layouts.app is required

Nash's avatar

Laravel gives you a starting point with Vue.js, Bootstrap and jQuery. You don't have to use these presets if you don't want to. Look at resources/assets/js/app.js and resources/assets/js/bootstrap.js and read the docs for more information: https://laravel.com/docs/5.6/frontend

In any case, jQuery needs to be loaded before your click handler.

nagendrabhat's avatar

Okay.. Thanks for the document which you have shared.

Please or to participate in this conversation.