Ajax with laravel

945 0 0 0

Last Updated : 2024-03-28 09:23:44

this snippet will explain how to create ajax requests with laravel specially with forms

to use ajax with laravel you must follow these steps 


1- add csrf meta tag to head in your html file


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

2- create your route to use it in the form like this


Route::post("sendMessage", ['uses'=>'MessageController@store', 'as'=>'sendMessage']);

3- create your form and assign your route to form action and specify form method like this


<form class="p-5 bg-white" id="messagingForm" action="{{ route('sendMessage') }}" method="post">  // assigned action and method here            
@csrf

<div class="row form-group">
<div class="col-md-12 mb-3 mb-md-0">
<label class="text-black" for="fname">???

Mahmoud Anwar

Mahmoud Anwar

Back End Developer with a passion for developing innovative web applications that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. Also has the proven ability to motivate, educate, and collaborate effectively to build web applications and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs, highlight their core competencies, and further their success.