- Laravel Application Development Cookbook
- Terry Matula
- 257字
- 2021-07-23 15:33:33
Gathering form input to display on another page
After a user submits a form, we need to be able to take that information and pass it to another page. This recipe shows how we can use Laravel's built-in methods to handle our POST data.
Getting ready
We'll need the simple form set up from the Creating a simple form section.
How to do it...
Follow these steps to complete this recipe:
- Create a route to handle the POST data from the form:
Route::post('userform', function() { // Process the data here return Redirect::to('userresults')- >withInput(Input::only('username', 'color')); });
- Create a route to redirect to, and to display the data:
Route::get('userresults', function() { return 'Your username is: ' . Input::old('username') . '<br>Your favorite color is: ' . Input::old('color'); });
How it works...
In our simple form, we're POSTing the data back to the same URL, so we need to create a route that accepts POST
using the same path. This is where we would do any processing of the data, including saving to a database or validating the input.
In this case, we simply want to pass the data to the next page. There are a number of ways to accomplish this. For example, we could use the Input
class's flashOnly()
method:
Route::post('userform', function() { Input::flashOnly('username', 'color'); return Redirect::to('userresults'); });
However, we're using a shortcut that Laravel provides, and only passing along two of the three form fields we asked for.
On the next page, we use Input::old()
to display the flashed input.
See also
- The Creating a simple form recipe
- 面向物聯(lián)網(wǎng)的CC2530與傳感器應(yīng)用開發(fā)
- 計算機(jī)網(wǎng)絡(luò)安全實訓(xùn)教程(第二版)
- 計算機(jī)網(wǎng)絡(luò)與數(shù)據(jù)通信
- 農(nóng)產(chǎn)品物聯(lián)網(wǎng)研究與應(yīng)用
- Drush User’s Guide
- 互聯(lián)網(wǎng)基礎(chǔ)資源技術(shù)與應(yīng)用發(fā)展態(tài)勢(2021—2023)
- 通信簡史:從信鴿到6G+
- 光纖通信系統(tǒng)與網(wǎng)絡(luò)(修訂版)
- Working with Legacy Systems
- 網(wǎng)管工具使用與技巧大全
- 物聯(lián)網(wǎng)的機(jī)遇與利用
- 智慧城市中的物聯(lián)網(wǎng)技術(shù)
- 物聯(lián)網(wǎng),So Easy!
- 無線傳感器網(wǎng)絡(luò)定位方法及應(yīng)用
- 互聯(lián)網(wǎng)視覺設(shè)計(全彩慕課版)