0

I'd like to know if it's possible to use Ajax with Laravel without writing the code inside a .js file.

Script.js

$('#selectSemestres').change(function(obj){
        var anoSemestre = $(this).val();
        $.ajax({
            type: 'GET',
            url: '',
            data: ...

        });
    })  

I don't know how to get the URL that I want (which is my currently page,
I just want to change the < select> value then reload the data that's already shown on the page).

Do I have to write the ajax inside my .php files? Is this a 'good practice'?
I'm asking this because I already have another script inside that same .php file.

Update

Following the answer posted by @ohgodwhy
Now nothing happens when the ajax executes.

It hits the success code but nothing happens, the URL does not change. Is it because I'm redirecting to the same page am at?

file.php

<script type="text/javascript">
    $('#selectSemestres').change(function(obj){
        var anoSemestre = $(this).val();
        $.ajax({
            type: 'GET',
            url: '{{ route('professor') }}',
            data: { anoSemestre: anoSemestre },
            success: function(){
                console.log('HELLO WORLD');
            }

        });
    })
</script>  

MyController:

public function getProfessorList()
    {
        $professor = Professor::all();
        if( Request::ajax() )
        {
            echo 'yolo';
        }
        $semestres = Horario::distinct()->select('ano_semestre')->get()->toArray();
        return View::make('professor', compact('professor', 'semestres'));
    }

1 Answer 1

1

What I usually do is add a @yield('scripts') section to my layout that I extend.

Then in the child templates that require specific JS, I'll add that in there.

@section('scripts')
     <script>
         //your javascript here.
     </script>
@endsection

There is 1 caveat however. If you use an @include from within a child template, and that included file has it's own javascript, it must invoke the parent first, like this:

@section('scripts')
    @parent
    <script>

    </script>
@endsection.
Sign up to request clarification or add additional context in comments.

3 Comments

So I do have to write it inside the php file, right? I always saw this as a bad practice or something that you should avoid
@PlayHardGoPro I disagree completely. If you have functionality specific to a given view - yes. Otherwise, if you can widgetize it so-to-speak feel free. Don't try to microoptimize your application out of the gate - write the code, get it bug free and functional, push it production. If you start making money, then hire some guys and go for a refactor. It's not a huge deal.
Followed your suggestion but now I'm getting an error. Any idea?

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.