0

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)?

Hedayatullah Sarwary
  • 2,664
  • 3
  • 24
  • 38
Milos
  • 552
  • 2
  • 7
  • 32
  • PHP variables are evaluated when the script is interpreted, but the user input is maintained AFTER the page was rendered. Why don't you use an HTML form and POST this data to the backend/controller? Or modify the `href` of the link with the user input via JavaScript? – Johannes Apr 09 '21 at 20:03
  • How can I do that? For example that one with href via JavaScript? – Milos Apr 09 '21 at 20:27

1 Answers1

1

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);
      });
    });
Johannes
  • 1,478
  • 1
  • 12
  • 28
  • This is working! Can you help with one more thing? I need that user can't go to the next page if it does not populate the input. Maybe some alert? – Milos Apr 09 '21 at 20:56
  • How is going to the next page triggered? By the `accept` button? – Johannes Apr 09 '21 at 21:03
  • Yes, by that `````` tag – Milos Apr 09 '21 at 21:10
  • There are several ways, please see https://stackoverflow.com/questions/10276133/how-to-disable-html-links (especially the `Intercept Links` section). However, I would also check in the backend if a valid `delivery` was passed and route back if not. Everything displayed in the browser can be manipulated by the user, i.e. with minor code changes she can also proceed to the next page. – Johannes Apr 09 '21 at 21:18