vnk
vnk

Reputation: 67

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

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('/', 'IndexController@index');

Route::get('/about', 'IndexController@about')->name('about');

Route::get('/contact','IndexController@contact')->name('contact');
Route::post('/contact','IndexController@thanks')->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

Upvotes: 1

Views: 824

Answers (1)

Ewan
Ewan

Reputation: 181

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']);

Upvotes: 1

Related Questions