在Laravel 5.6中提交之前如何显示表单数据以供预览

时间:2018-08-25 18:17:06

标签: php laravel forms

我的视图有一个简单的表格。我想在提交给数据库之前将这些数据显示给用户查看。我正在使用Laravel 5.6

<form action="{{url('/posts')}}" method="POST">
    @csrf
    <div class="form-group">
      <label for="title">Title of your Post</label>
      <input type="text" class="form-control"  placeholder="Post Title Goes here" name="title">     
    </div>
    <div class="form-group">
        <label for="body">Post Details</label>
        <textarea class="form-control" id="summernote"  placeholder="Detail of Post Goes here" name="body"></textarea>     
      </div>
    <button type="submit" class="btn btn-primary">Save</button>
  </form>   

如果用户要进行更改,我还希望用户对其进行编辑。

控制器:

public function store(Request $request)
    {
        //validate the form
        $this->validate($request, [
            'title' => 'required',
            'body' => 'required'
        ]);

        //save data in database
        $data = $request->all();
        if(!empty ($data)){
            try{
                $newpost = new Post();
                $newpost->title = $data['title'];
                $newpost->body = $data['body'];
                $newpost->save();
                return redirect('/posts')->with('success', 'Record Added Successfuly');
            }catch(\Exception $e){
               // echo $e;
            }

        }//if ends here

    } 

1 个答案:

答案 0 :(得分:0)

PHP方法

使用确认URL,它可以是获取或发布,我宁愿使用发布

Route::post('/posts/confirmation', 'PostController@confirmation');

将此添加到您的控制器

public function confirmation(Request $request)
{
    //validate the form
    $this->validate($request, [
        'title' => 'required',
        'body' => 'required'
    ]);

    $data['myForm'] = $request->all();

    return view('posts.confirmation', $data)

}

您的确认视图应如下所示:

<div>
   // your displaying view goes here
</div>
<form action="{{url('/posts')}}" method="POST">
    @csrf
    <input type="hidden" name="title" value="{{ $myForm->title }}">     
    <textarea style="display:none;" name="body">{{ $myForm->body }}</textarea>     
    <button type="submit" class="btn btn-primary">Save</button>
</form>