Arkhall Arkhall - 3 months ago 8
PHP Question

Laravel - Get a data from a form

I'm working on a form (without MySQL for the moment) in order to get a data from an input with a confirm message page like "Congratulations $pseudo, your subscribe is successful".

Problem : I can't display the data what I inserted into my form.

Guideline : I think the problem is on my controller in the postUsers method but I don't know how to resolve it.

Here is my form (subscribe.blade.php)

@extends('template')

@section('contenu')
<div class="container">
<h1>Inscription</h1>
{!! Form::open(['url' => 'users/confirm']) !!}
<div class="form-group has-feedback {!! $errors->has('pseudo') ? 'has-error' : '' !!}">
<label for="pseudo">Pseudonyme</label>
{!! Form::text('pseudo', null, ['class' => 'form-control', 'id' => 'id_pseudo', 'placeholder' => 'Votre pseudonyme']) !!}
{!! $errors->first('pseudo', '<small class="help-block">:message</small>') !!}
</div>
{!! Form::submit('Inscription', ['class' => 'btn btn-default']) !!}
{!! Form::close() !!}
</div>
@endsection


Here is the confirm page (confirm.blade.php)

@extends('template')

@section('contenu')
<br>
<div class="col-sm-offset-3 col-sm-6">
<div class="panel panel-info">
<div class="panel-heading">Validation</div>
<div class="panel-body">
Féicitations<?php Request::input('pseudo')?>, vous êtes inscrit sur le site ! Vous pouvez dès à présent vous connecter.
</div>
</div>
</div>
@endsection


Here is my routes (routes.php)

Route::get('users', 'UsersController@getUsers');
Route::get('users', 'UsersController@postUsers');


And the controller (UsersController.php)

class UsersController extends Controller
{
public function getUsers(){
return view('confirm');
}

public function postUsers(Request $request){
return 'Le nom est ' . $request->input('pseudo');
}
}


I hope it can helps you to resolve this little problem, I'm following a course about laravel and this framework is facinating for me ^^

Thank you a lot for taking time on my problem. Have a nice day :)

Answer

You should use POST instead of GET when you're submitting form. Also, you're using wrong URL, so try to change this:

Route::get('users', 'UsersController@postUsers');

to this:

Route::post('users/confirm', 'UsersController@postUsers');

If you want to display data in confirm view, do something like this:

public function postUsers(Request $request){ 
    return view('confirm', ['pseudo' => $request->pseudo]);
}

and view:

....
<div class="panel-body">
    Féicitations {{ $pseudo }}, vous êtes inscrit sur le site ! Vous pouvez dès à présent vous connecter.
</div>
....
Comments