如何将值从输入表单传递到laravel中的另一个页面

时间:2018-02-24 11:13:51

标签: php html web laravel-5 netbeans

不好意思,我是学习Laravel的新手,我对表单输入显示数据值有疑问。 我有create.blade.php:

@extends('layouts.master')

@section('content')

<div class="container">
    <div class="header">
        <h1><b>Create an account</b></h1>
        <h5>Welcome to Konoha Village</h5>
    </div>
    {{ csrf_field() }}

    @if(isset($name))
    <div class="alert alert-warning alert-dismissible" role="alert">
        Halo <strong>{{$name}}</strong>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">$times;</span>
        </button>
    </div>
    @endif

    <div class="form">
        <form action="{{ url('final-test') }}" method="post" id="form1">
            <div class="form-group">
                <input name="name" id="name" class="form-control" placeholder="Your Full Name"/>                
            </div>
            <br>
            <div class="form-group">
                <input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
            </div>

            <div class="form-group">
            </div>

        </form>
    </div>
</div>
@endsection

我的控制器名为AccController.php:

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AccController extends Controller
{
    public function create() {
        return view('home.create');
    }

    public function show(Request $r) {
        $soul = $r ->name;

        $pesan = "Your name is {$r->name}";
    }
}

和我在web.php中的路线:

//route to get play form
Route::get ('start',        'AccController@create' )->name('home.create');
Route::post('final-test',  'AccController@show');

我想在另一个页面视图中显示我调用了show.blade.php:

@extends('layouts.master')

@section('content')

<div>
    {{$name = Input::get('name')}}
    <h1>Your name is {{ $pesan }}</h1> </div>   

@endsection
最后没有任何错误,但它无法显示输入表单中的值,请问我帮忙吗? 此致,Aga。

2 个答案:

答案 0 :(得分:0)

您不是在这里遵守惯例。 show方法是来自数据库的数据。为了显示表单数据,您已在store方法中执行此操作
HTML代码

html表单应该有一些小的改动

{{--Changing in just action--}}
{{-- If it doesn't accept that action then replace it with {{AccController.php@store}} --}}
<form action="AccController.php@store" method="post" id="form1">
        <div class="form-group">
            <input name="name" id="name" class="form-control" placeholder="Your Full Name"/>                
        </div>
        <br>
        <div class="form-group">
            <input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
        </div>

        <div class="form-group">
        </div>

    </form>

然后在Controller中,您将获得商店功能

中的表单数据
 public function store(Request $r) {
    $soul = $r ->name;

    $pesan = "Your name is {$r->name}";
    return $pesan;

}

如何在路线上获取它?

你在route.php中犯了一些错误。你只需遵循laravel为我们提供的惯例。

用此替换您的路线代码。

    Route::resource('/posts', 'AccController.php');

这里它会自动调用Controller的所有默认功能,并为它们分配特定的路由。例如在图片下方

enter image description here

您只需在终端或命令提示符下键入php artisan route:list,您将看到已创建的路由列表以及laravel为您创建的路由列表以及方法,URI和名称。您只需遵循惯例,它将自动为您提供结果 试一试告诉我

答案 1 :(得分:0)

您的web.php

//route to get play form
Route::get('/start', 'AccController@create')->name('home.create');
Route::post('/final-test', 'AccController@show')->name('home.show');

在路线最终测试中添加了名称home.show

您的AccController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AccController extends Controller
{
    public function create()
    {
        return view('home.create');
    }

    public function show(Request $r)
    {
        $soul = $r->name;
        return view('home.show')->with('soul', $soul);
    }
}

您的show.blade.php

@extends('layouts.master')

@section('content')
<div>
    <h1>Your name is {{ $soul }}</h1>
</div>
@endsection

您的create.blade.php

@extends('layouts.master')

@section('content')
<div class="container">
    <div class="header">
        <h1><b>Create an account</b></h1>
        <h5>Welcome to Konoha Village</h5>
    </div>

    @if(isset($name))
    <div class="alert alert-warning alert-dismissible" role="alert">
        Halo <strong>{{$name}}</strong>
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">$times;</span>
        </button>
    </div>
    @endif

    <div class="form">
        <form action="{{ route('home.show') }}" method="post" id="form1">
            {{ csrf_field() }}
            <div class="form-group">
                <input name="name" id="name" class="form-control" placeholder="Your Full Name"/>
            </div>
            <br>
            <div class="form-group">
                <input style="cursor:pointer" type="submit" class="btn btn-primary" id="submit" value="Show into Dashboard">
            </div>

            <div class="form-group">
            </div>

        </form>
    </div>
</div>
@endsection

以下是最重要的更改:我在表单中移动{{ csrf_field() }},因此您无法获取The page has expired due to inactivity. Please refresh and try again.并将表单操作更改为指定路由{{ route('home.show') }}

我保留了您的<span aria-hidden="true">$times;</span>,但这只会显示$times;,可能需要调整一下。