In this tutorial we will discuss how to send one page form data to another page without form submit. Send a variable one page to another page. How to pass form data to another page in laravel.
Here we send one page data to another page by set in session with jquery and ajax. save data in session by jquery ajax.
Send Form data to another page
When the user clicks on submit button we send form values to a controller by ajax and set them in session after success page redirects to other page by location.href and here print values by session.
Routes
Route::get('event-form', [BaseController::class, 'form'])->name('form'); Route::get('setData', [BaseController::class, 'setData'])->name('setData'); Route::get('other-page', [BaseController::class, 'otherPage'])->name('otherPage');
Controller
Here our BaseController controller where setData() method set session variable throw ajax method and otherPage print amount value.
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Session; class PostController extends Controller { public function form() { return view('form'); } public function setData(Request $request) { $name = $request->name; $amount = $request->amount; Session::put('name', $name); Session::put('amount', $amount); return true; } public function otherPage() { return view('other_page'); } }
Views
form.blade.php view file<!DOCTYPE html> <html> <head> <title>Laravel set session by jquery ajax</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" ></script> </head> <body> <h1>Test form for sending value to other page</h1> <form> <input type="text" name="name" id="name"> <input type="text" name="amount" id="amount"> <button id="submit">Submit</button> </form> <script> $(".submit").click(function(e){ e.preventDefault(); var name = $("#name").val(); var amount = $("#amount").val(); $.ajax({ type:'GET', url:"{{ route('setData') }}", data:{name:name, amount:amount}, success:function(data){ location.href = "{{ route('otherPage') }}"; } }); }); </script> </body> </html>other_page.blade.php view file
<!DOCTYPE html> <html> <head> <title>Laravel get another page data</title> </head> <body> <h1>Form values are</h1> <p><b>Name :</b>{{ Session::get('name') }}</p> <p><b>Amount :</b>{{ Session::get('amount') }}</p> </body> </html>