Milos
Milos

Reputation: 612

Laravel - how to pass variable that value is equal to input value in view(blade)?

I'm using Laravel 5.8. In this blade, I have text input that the user must populate and after that to go to other page view and value from that input must be passed on that other page view

Here is my text input code:

<input type="text" name="delivery_time" value="">

And here is my <a> tag for a link to another page:

<a href="{{route('manager.invoicePrint', ['id' => $restaurant->id, 'code' => $inv->id, 'delivery' => 'MINUT'])}}" class="btn btn-success btn-block font-weight-bold">Prihvati</a>

So I'm already passing that variable delivery and its value ' MINUT' but I need that variable to be equal to the input that the user populate.

And this is a invoicePrint function for that other page view:

/**
* Show the form for creating a new resource.
*
* @param  int  $id
* @return \Illuminate\Http\Response
*/
public function invoicePrint($id, $code, $delivery)
{
   $restaurant = Restaurant::find($id);
   $inv = Invoice::findOrFail($code);

   if(Auth::user()->userRestaurants->first()->id != $restaurant->id){
      return redirect()->back();
   }

   return view('website.restaurants.dashboard.invoice-print', compact('restaurant', 'inv', 'delivery'));
}

Here I'm passing delivery variable, and in my web.php:

Route::get('/narudzbina/{id}/{code}/{delivery}', 'RestaurantsController@invoicePrint')->middleware('auth')->name('manager.invoicePrint');

This Route is in two Route::group.

How can I pass input value to the variable and then that variable pass on another page view(blade)?

Upvotes: 0

Views: 1612

Answers (1)

Johannes
Johannes

Reputation: 1488

The input field gets an id test-input for making accessing it's value easier. In addition, we store the generated route (without the {delivery}) as a data attribute for the link.

<a href="/" data-baseroute="/{id}/narudzbina/{code}/" class="btn btn-success btn-block font-weight-bold" id="the-link">Prihvati</a>
 <input id="test-input"/>

For sake of simplicity, I'm using jQuery here. We need to register a change event handler on the input field so that every typed character needs to be added to the href immediately. Please be aware that not every character is supported in URL's, hence you need to sanitize the user input first.

    $(document).ready(function() {
      $('#test-input').on('change', function(e) {
        var linkElement = $('#the-link');

        // get value user typed in
        var inputFieldValue = e.target.value;
        // compose route using baseRoute (generated by Laravel)
        var baseRoute = linkElement.data('baseroute');
        // set it as href
        linkElement.attr('href', baseRoute + inputFieldValue);
      });
    });

Upvotes: 1

Related Questions