【问题标题】:Display form input values from one page to another从一页到另一页显示表单输入值
【发布时间】:2017-02-01 06:00:48
【问题描述】:

我有两 (2) 页。一个是创建页面,另一个是确认页面。创建页面有一个表单和一个“继续确认”按钮,该按钮将重定向到确认页面。确认页面应显示来自创建页面的所有输入,然后用户在检查完所有输入值后即可提交。

控制器:

public function create()
{
    return ('pages.create');    
}

public function confirmCreate()
{
    $value = Input::all();
    Session::flash('value', $value);
    return view('pages.confirm-create')->with('value', $value);
}

路线:

Route::get('create', 'MyController@create')->name('create');

Route::post('confirm-create', 'MyController@confirmCreate')->name('confirm-create');

Route::put('store', 'MyController@store')->name('store');

创建页面视图:

{{ Form::open(array('url' => 'confirm-create',
                                'method' => 'POST', 
                                'class' => 'form-horizontal')) }}

    <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
      <label class="col-md-4 control-label required">Name</label>
      <div class="col-md-6">
        <input id="name" placeholder="Name" type="text" class="form-control" name="name" value="{{ old('name) }}" required autofocus>
        @if ($errors->has('name'))
        <span class="help-block">
          <strong>{{ $errors->first('name') }}</strong>
        </span>
        @endif
      </div>
    </div>

    <div class="form-group{{ $errors->has('contactNumber') ? ' has-error' : '' }}">
      <label class="col-md-4 control-label required">Contact Number</label>
      <div class="col-md-6">
        <input id="contactNumber" placeholder="Contact Number" type="text" class="form-control" name="contactNumber" value="{{ old('contactNumber) }}" required autofocus>
        @if ($errors->has('contactNumber'))
        <span class="help-block">
          <strong>{{ $errors->first('contactNumber') }}</strong>
        </span>
        @endif
      </div>
    </div>

    <div class="form-group">
      <div class="col-md-6 col-md-offset-6">
        <button type="submit" class="btn btn-primary">
          Proceed to Confirm
        </button>
      </div>
    </div>

{{ Form::close() }}

确认页面查看:

{{ Form::open(array('url' => 'pages',
                                'method' => 'POST', 
                                'class' => 'form-horizontal')) }}

    <div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
      <label class="col-md-4 control-label required">Name</label>
      <div class="col-md-6">
        <label class="control-label">{{ old('name') }}</label>
        @if ($errors->has('name'))
        <span class="help-block">
          <strong>{{ $errors->first('name') }}</strong>
        </span>
        @endif
      </div>
    </div>

    <div class="form-group{{ $errors->has('contactNumber') ? ' has-error' : '' }}">
      <label class="col-md-4 control-label required">Contact Number</label>
      <div class="col-md-6">
        <label class="control-label">{{ old('contactNumber') }}</label>
        @if ($errors->has('contactNumber'))
        <span class="help-block">
          <strong>{{ $errors->first('contactNumber') }}</strong>
        </span>
        @endif
      </div>
    </div>

    <div class="form-group">
      <div class="col-md-6 col-md-offset-6">
        <button type="submit" class="btn btn-primary">
          Submit
        </button>
      </div>
    </div>

{{ Form::close() }}

我似乎无法从我的创建页面(单击“继续确认”按钮后)将数据显示到我的确认页面。我的控制器、路由和视图(创建和确认页面)应该是什么样子?发送帮助。谢谢!

【问题讨论】:

    标签: php forms laravel laravel-5


    【解决方案1】:

    您将value 传递给pages.confirm-create 视图但不使用它。

    {{ old('contactNumber') }} 替换为{{ $value['contactNumber'] }}{{ $value['contactNumber'] }}

    当您进行验证时使用old() 很好,它从先前的请求而不是从传递给视图的值中获取值。

    【讨论】:

    • 谢谢。我按照你说的做了,但我的确认页面仍然是空的。我的创建页面操作网址 (url =&gt; 'confirm-create') 是否正确?
    • 另外,我认为我的控制器仍然缺少某些东西或仍然不正确。
    • confirm-create.blade.php 的顶部添加&lt;?php var_dump($values); ?&gt; 以验证它是一个数组。
    【解决方案2】:

    您在Confirm Page 视图上犯了一个小错误。

    不能这样使用旧输入。查看文档:

    https://laravel.com/docs/5.3/requests#old-input

    其中规定:

    Laravel 允许您在下一个请求期间保留来自一个请求的输入。此功能对于在检测到验证错误后重新填充表单特别有用。

    它仅用于发生验证问题的同一表单。

    你也在使用:

    <label class="control-label">{{ old('name') }}</label>
    

    而不是输入字段。

    <input id="name" placeholder="Name" type="text" class="form-control" name="name" value="{{ $value['name'] }}" required autofocus>
    

    您需要使用$value['name'] 而不是old('name')

    【讨论】:

    • 感谢您的回答。但是我在创建页面中使用了&lt;input&gt;,而不是在确认页面中。我在确认页面中使用了&lt;label&gt;
    • 好吧。那么,您是否尝试过测试该值是否出现在您的视图中?您可以通过{{ dd($value) }} 来检查是否有任何值已通过
    猜你喜欢
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2018-05-28
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多