简体   繁体   English

Laravel - 如何在视图(刀片)中传递值等于输入值的变量?

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

I'm using Laravel 5.8.我正在使用 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在这个刀片中,我有用户必须填充的text input ,然后到 go 到其他页面视图,并且来自该输入的值必须在其他页面视图上传递

Here is my text input code:这是我的text input代码:

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

And here is my <a> tag for a link to another page:这是我的<a>标记,用于链接到另一个页面:

<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.因此,我已经传递了该变量delivery及其值' MINUT' ,但我需要该变量等于用户填充的输入。

And this is a invoicePrint function for that other page view:这是另一个页面视图的invoicePrint function:

/**
* 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 :在这里,我传递了delivery变量,并在我的web.php中:

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

This Route is in two Route::group .这条Route在两个Route::group中。

How can I pass input value to the variable and then that variable pass on another page view(blade)?如何将输入值传递给变量,然后该变量传递给另一个页面视图(刀片)?

The input field gets an id test-input for making accessing it's value easier.输入字段获得一个 id test-input ,以便更轻松地访问它的值。 In addition, we store the generated route (without the {delivery} ) as a data attribute for the link.此外,我们将生成的路由(没有{delivery} )存储为链接的数据属性。

<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.为简单起见,我在这里使用 jQuery。 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.我们需要在输入字段上注册一个change事件处理程序,以便每个键入的字符都需要立即添加到href Please be aware that not every character is supported in URL's, hence you need to sanitize the user input first.请注意,并非 URL 中支持每个字符,因此您需要先清理用户输入。

    $(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);
      });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM