官术网_书友最值得收藏!

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:

  1. 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'));
    });
    
  2. 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
主站蜘蛛池模板: 荃湾区| 社旗县| 自贡市| 英山县| 南乐县| 锦州市| 舟曲县| 锦屏县| 泰兴市| 兴城市| 富宁县| 太仓市| 板桥市| 西华县| 察隅县| 仙居县| 怀安县| 余庆县| 嘉定区| 云阳县| 博野县| 高安市| 滦平县| 吉林市| 南漳县| 民丰县| 乌拉特后旗| 合阳县| 宁波市| 成武县| 剑河县| 措勤县| 会宁县| 杭锦旗| 富锦市| 西丰县| 库尔勒市| 囊谦县| 泰和县| 肥东县| 崇信县|