Home » Php » php – How to pass value from input textbox to another page in Laravel

php – How to pass value from input textbox to another page in Laravel

Posted by: admin February 25, 2020 Leave a comment

Questions:

The indexcontroller is shown below

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function index()
    {
        return view('welcome');
    }

    public function about()
    {
        return view('about');
    }

  public function contact()
  {
    return view('contact');
  }

  public function thanks(Request $request)
  {
    $request->validate(['firstname' => ['required', 'alpha_num']]);
    return view('thanks');
  }


}

contact.blade.php is below

@extends('welcome')
@section('content')
          <div>
            <h2>Contact Page</h2>
            <form method="post" action="{{route("thanks")}}">
              @csrf<input type="text" name="firstname"/>
              <input type="submit"/>
            </form>
            @if ($errors->any())
            <div class="alert alert-danger">
              <ul>
                @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
                    @endforeach
              </ul>
            </div>
                @endif
                  </div>
  @endsection

Thanks.blade.php

@extends('welcome')
@section('content')
<div>
  <h2>Thanks</h2>Thank you {{ $firstname }}
</div>
@endsection

welcome.blade.php

  <div class="flex-center position-ref full-height">
            <div class="content">
                <div class="title m-b-md">
                    app
                </div>

                <div class="links">
                    <a href="{{ route('about')  }}">About</a>
                    <a href="{{ route('contact')  }}">contact</a>
                </div>
                <h1>{{$firstname}}</h1>
                <div>
                    @yield('content')
                </div>

            </div>
        </div>

web.php

<?php



Route::get('/', '[email protected]');

Route::get('/about', '[email protected]')->name('about');

Route::get('/contact','[email protected]')->name('contact');
Route::post('/contact','[email protected]')->name('thanks');

When I click on contact in welcome.blade.php it takes me to contact page where the textbox is pressent . The value entered should appear in thankyou.blade.php.
I need the value entered in the textbox to be shown in thanks.blade.php when I click on submit.
Thanks in Advance

How to&Answers:

Data needs to be passed to a view explicitly. For hopefully obvious reasons, it isn’t globally accessible as a variable.

E.g.

return view('thanks', ['firstname' => 'Your First Name']);