Laravel - 从表单中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38803466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:15:35  来源:igfitidea点击:

Laravel - Get a data from a form

phplaravelcontroller

提问by

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".

我正在处理一个表单(目前没有 MySQL),以便从带有确认消息页面的输入中获取数据,例如“恭喜 $pseudo,您的订阅成功”。

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.

指南:我认为问题出在 postUsers 方法中的控制器上,但我不知道如何解决。

Here is my form (subscribe.blade.php)

这是我的表格(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)

这是确认页面(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)

这是我的路线(routes.php

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

And the controller (UsersController.php)

和控制器(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 ^^

我希望它可以帮助您解决这个小问题,我正在学习有关 Laravel 的课程,这个框架对我来说很有趣^^

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

非常感谢您花时间解决我的问题。祝你今天过得愉快 :)

采纳答案by Alexey Mezenin

You should use POSTinstead of GETwhen you're submitting form. Also, you're using wrong URL, so try to change this:

您应该在提交表单时使用POST而不是GET。另外,您使用了错误的 URL,因此请尝试更改:

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

to this:

对此:

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

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

如果要在confirm视图中显示数据,请执行以下操作:

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>
....