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

mozew's avatar
Level 6

like /public/js/myjavascript.js

Each page has a form tag.

Cronix's avatar

like /public/js/myjavascript.js

Bingo. There's your problem.

You are using blade syntax and php code in your js file, which you can't do.

So this won't work

$.post("{{ route('schools-list.store') }}", ...

{{ }} is blade syntax, only parsed by php if it's in a laravel blade.php file. route() is a laravel php helper function. It won't work in a .js file!!

Change it like I mentioned a few pages ago so it doesn't use blade or php.

$post('/admin/schools-list', ...

Then force refresh your browser, or clear your browsers cache or it might not pick up on your changes.

jlrdw's avatar

@irankhosravi honest I am not trying to confuse matters here, but with all the recent replies on a successful Ajax post you're not willing to read some of them and study how it was done.

There have been quite a few in fact over the last month very similar to what you are doing.

Fyi YouTube also has many tutorials on using jQuery ajax.

And many Developers will alert out things at different points to ensure things are working.

Cronix's avatar

Hmm, I changed it following this code but I have yet problem.

Then force refresh your browser, or clear your browsers cache or it might not pick up on your changes.

jlrdw's avatar

You do not listen to advice like have you even alerted anything anywhere to test.

I am guessing no.

Hitostacha's avatar

@irankhosravi did you check your route is post? Like so: Route::post('/url','controller@method')->name('route.name');

1 like
jlrdw's avatar

@irankhosravi you are working with quite a few fields, wouldn't it be easier to just work with two fields until you resolve the problem then and all those other fields.

are you sure you are passing the csrf token and as suggested check your route.

Did you even bother to look at the guide I posted yes it's a simple guide but it works.

In fact the only time I've ever had trouble posting with Ajax using laravel was the exact same thing I forgot the csrf token.

Hitostacha's avatar

@jlrdw the csrf token isn't necessary, I just worked on a project with Laravel and Ajax posts requests all over the place, it's just not safe to not use csrf tokens because it protects against malicious requests from other websites using your routes as far as I know

Here's an example of my posts requests in the following order (Form, js, controller and routes).

Form in Laravel(it's actually not necessary to put everything in a form because you append the data on a javascript object, but if you've got no other methods to validate fields, then it's fine )

<form action="post_function($event)">
<input type="text" id="name">
<input type="text" id="lastname">
<input type="submit">
</form>

JS Function

function post_function(e){
    e.preventDefault(); //So your page doesn't get updated
    var data = new FormData(); //I got used to working with formdatas, you can send just a normal objects if you don't like it

data.append('name', document.getElementById('name').value);
data.append('lastname', document.getElementById('lastname').value);

$.ajax({
    url: '/api/person'
    method: 'POST',
    data: data,
    processData: false,
        contentType: false,
    beforeSend: function(){
    //TODO handle the loading status, its not necessary
    },
    success: function(res){
    //TODO handle success response
    },
    error: function(err){
    //TODO handle error response
    }
});
}

On your controller

public function store(Request $request){
    //Just handle everything as you did last time, you've got nothing wrong on your code as far as I know
}

On the routes, check as I stated before, it like sending the request on an API route instead of a web route because after all you're just sending data and not expecting a view response

Route::post('/person','controller@store')->name('route.name');
jlrdw's avatar

@Hitostacha what? I never said do not use csrf token I asked him is he sure he is using it in the Ajax.

And really with all the past examples of using an Ajax post right here on this forum almost anyone should be able to figure out how to correctly do it.

Many of the past answers are like mini tutorials.

hanshuang's avatar

you may try adding this. $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });

Previous

Please or to participate in this conversation.