繁体   English   中英

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

[英]how to pass value from input form into another page in laravel

对不起,我是新来的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

最后没有任何错误,但无法显示输入表单中的值,请您帮我吗? 问候,阿加。

您未在此处遵循约定。 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的所有默认功能,并为其分配特定的路由。 例如下面的图片

在此处输入图片说明

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

您的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. The page has expired due to inactivity. Please refresh and try again. {{ route('home.show') }}表单操作更改为命名路线{{ route('home.show') }}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM