Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

php - Laravel Passing Data From One View to Another View

I am thinking of making a landing page from the home page, which will direct the guest to the register page. I thought of making two forms for sending data and two submit buttons in them, let's say reader and writer and according to the button they use to go to the register form page, I want to pass the profession string from the button in the landing page and then, place it into the register form in /auth/register.

    {!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}

    {!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}

    {!! Form::close() !!}


    {!! Form::open(array('url' => '/auth/register', 'profession' => 'reader')) !!}

    {!! Form::submit('Reader', array('class' => 'btn btn-default')) !!}

    {!! Form::close() !!}

It is not directing me to the page app.com/auth/register. But it works when I directly type the link.

What I thought was using $profession in /auth/register/ and access the value and use it as a hidden field in the registeration form.

(using laravel 5.1)


Edit:

In view source:

    <form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="writer"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">

    <input class="btn btn-warning" type="submit" value="Writer">

    </form>


    <form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="reader"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">

    <input class="btn btn-default" type="submit" value="Reader">

    </form>

Edit 2:

    {!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}

    {!! link_to('/auth/register', 'Writer', array('class' => 'btn btn-default')) !!}

    {!! Form::close() !!}

I tried this instead. At least, now it is directing the page but I still can't access the data value of profession


Edit 3:

Routes:

Route::get('auth/register', 'AuthAuthController@getRegister');
Route::post('auth/register', 'AuthAuthController@postRegister');

Route::get('/', function()
{
    return view('pages.home');
});

and https://app.com/auth/register is working.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a step by step walkthrough on how to implement it. I tested it. So it works. This is for 'writer', but you could replicate it as you had originally planned for other professions.

I assume you've registered the Laravel Collective package since you're using the curly braces and exclamation points.

Step 1:

In your landing page view, where you have the writer button, add a hidden field with the string 'writer'. Like this:

{!! Form::open(['route' => ['writer_path']]) !!}
{!! Form::hidden('profession', 'writer') !!}
{!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}

Not that in the open field we are using a named route ('writer_path').

Step 2:

Register a route and a controller on your routes.php file, like this:

Route::post('auth/register', [
'as' => 'writer_path',
'uses' => 'SampleController@displayForm'
]);

Step 3:

In your sample controller, you define the displayForm method. Within that method you first obtain the value you passed from the landing page view.

If you don't know how to create a controler, you can do

php artisan make:controller SampleController

from the command line

Because the value arrives as an array, you have to obtain the string 'writer' from the array and then pass it to the new view (the view with the registration form for the writer).

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;
use IlluminateSupportFacadesInput;

class SampleController extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return Response
 */
public function displayForm()
{
    $input = Input::get();
    $profession = $input['profession'];
    return view('writerregistration', ['profession' => $profession]);
}

}

Last Step:

In the new view which you will create as writerregistration.blade.php, you will display the form with the field you just passed ('profession') which contains the string 'writer'. Like this:

{!! Form::open() !!}

{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class' => 'form-control']) !!}

{!! Form::label('profession', 'Profession:') !!}
{!! Form::text('profession', $profession, ['class' => 'form-control']) !!}

{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}

{!! Form::label('passowrd', 'Password:') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}  

{!! Form::label('password_confirmation', 'Password Confirmation:') !!}
{!! Form::password('password_confirmation', ['class' => 'form-control']) !!}

{!! Form::submit('Sign Up', ['class' => 'btn btn-primary']) !!}

{!! Form::close() !!}

Presto, you've populated the field in the registration form for the writer with the info on the hidden field that belonged to the writer button in the landing page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...