【问题标题】:Laravel, Pass input variables from page to other using sessionLaravel,将输入变量从页面传递到其他使用会话
【发布时间】:2018-04-13 23:43:12
【问题描述】:

请注意,我是 Laravel 的新手,所以我需要认真的帮助,因为 3 天以来一直在寻找解决方案,但没有运气。 我只需要查看或显示从预订表单到客户/仪表板页面的输入文本的值,请注意,在您登录之前无法发送输入数据,这工作正常,但是如何传递数据并显示在下一页? 请用代码回答

表格:

   <form id="booking-form">
 <input class="indexinput" type="text" id="country" name="country" 
 placeholder="Country">
<input class="indexinput" type="text" id="state" name="state" 
placeholder="State">
<input class="indexinput" type="text" id="city" name="city" placeholder="city">
</form>

 <button class="button getbids" >GET BIDS</button> 


 <script type="text/javascript">
  $(document).on('click', '.getbids', function() {
  var loggedIn = {{ auth()->check() ? 'true' : 'false' }};
   if (loggedIn)
  window.location.replace('customer/dashboard');
  if(!loggedIn)
  $('#loginModal').modal('show'); 
   });
   </script>

路线

  Route::group(['middleware'=>['rolecustomer']], function () {

  Route::get('/customer/dashboard', 'HomeController@index')->name('customer.dashboard');
 });

控制者:

  public function index()
   {
   return view('index.customer.customerdashboard');
     }

【问题讨论】:

标签: php laravel


【解决方案1】:

要将值存储在会话中,请将输入的值传递给 laravel 的会话,如下所示。

Session::set('input_field', 'field_value');

要检索会话,请使用以下代码。

Session::get('input_field');

更多关于 laravel session here 的信息。

【讨论】:

  • 我有输入: 所以我必须把 `Session: :set('input_field', 'filed_value'); input_field 是什么意思?是国家吗?身份证=国家?你能清楚我必须把你写的每一行放在哪里吗??
  • 所以基本上 laravel 会设置像 key-value 这样的会话值。在上面的示例中,input_field 是我使用的 key 名称。它可以是任何东西,可以是你的名字,输入的名字,任何你想调用的键。而第二个参数filed_value就是你需要传递的值。在您的情况下,请传递 input value 或您想要传递的值。提交后,将使用密钥名称创建会话。加载下一页时,使用您设置的键名检索会话值。就像这里的例子一样,它的input_field
【解决方案2】:

如果你想在整个会话中永久放置一些东西,你可以使用$session-&gt;put('key', 'value')

在你的情况下,

public function bids(Request $request){

    $request->session()->put('country', $request->country);
    $request->session()->put('state', $request->state);
    $request->session()->put('city', $request->city);
    return redirect()->route('route_name');

}

然后在下一页中,使用Session::get($key)

<div>
   <p>Country : {{ Session::get('country') }}</p>
   <p>State: {{ Session::get('state') }}</p>
   <p>City: {{ Session::get('city') }}</p>
</div>

或者,更方便的访问 Session 的方法是首先通过 Session::has('key_name') 检查值是否在 Session 中。

<div>
   <p>Country : {{ (Session::has('country') ? Session::get('country') : '' ) }}</p>
   .....
</div>

【讨论】:

  • 没用!它复杂我必须在 web.php 中做什么?路线是我的问题,我是否必须修改动作和方法等表单?
猜你喜欢
  • 2012-06-17
  • 2017-02-12
  • 1970-01-01
  • 2017-05-21
  • 2017-09-11
  • 2013-05-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多