简体   繁体   English

无法从表单 LARAVEL 获取输入数据

[英]Can't get input data from form LARAVEL

I'm learning Laravel and I got stuck trying to get data from a form.我正在学习 Laravel,但在尝试从表单中获取数据时遇到了困难。

I already am able to get data back with GET, but with POST I've been having a ton of trouble.我已经能够使用 GET 取回数据,但是使用 POST 我遇到了很多麻烦。 Here's what I'm working with:这是我正在使用的内容:

Form:形式:

<form id="forms" method="POST" action="sugestoes" novalidate>

  {{ csrf_field() }}

  <div class="form-row">

    <div class="form-group col-md-12">
      <label for="obs">Observações:</label>
      <textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
    </div>

  </div>

    <hr>

  <button type="submit" class="btn btn-primary">Enviar</button>

</form>

  @php 

  if (isset($_POST["obs"])) {
    echo "IN";
  }

  @endphp

Controller:控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{

    public function store(Request $request)
    {
        $name = $request->input('obs');

        return redirect('sugestoes');


        //
    }
}

Route:路线:

Route::post('sugestoes', 'PostController@store');

The intended behaviour that I'm trying to reach is for the post to be submitted, and then returning to the same page with an empty form.我试图达到的预期行为是提交帖子,然后使用空表单返回同一页面。 Later on I'll be sending the input data into a database, but for now I just want to get the post to work.稍后我会将输入数据发送到数据库中,但现在我只想让帖子工作。

I guess I'm missing something really basic, but I've been following guides and looking online, I've done some progress but I'm really stuck here.我想我错过了一些非常基本的东西,但我一直在遵循指南并在线查看,我已经取得了一些进展,但我真的被困在这里。

(some more info, this is Laravel 5.4, and I'm using XAMPP) (更多信息,这是 Laravel 5.4,我使用的是 XAMPP)

First, you need to call the model, use App/Your_model_name;首先需要调用模型, use App/Your_model_name; then you have to save the data.然后你必须保存数据。

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Suggest; //Suggest model, let's hope you have suggest table

class PostController extends Controller
{

  public function store(Request $request)
  {
    $suggest = new Suggest; //model
    $suggest->name = $request->obs; //name is DB name, obs is request name
    $suggest->save(); //save the post to DB

    return redirect()->back()->with('success', 'Saved successfully'); //return back with message
  }
}

Then if you want to flash the message on the HTML page然后,如果您想在 HTML 页面上闪烁消息

@if(session('success'))
  <div class="alert alert-warning alert-dismissible" id="error-alert">
     <strong style="color: white;">{{session('success')}}</strong>
  </div>
@endif
<form id="forms" method="POST" action="{{ route('sugestoes') }}" novalidate>
 {{ csrf_field() }}
<div class="form-row">

<div class="form-group col-md-12">
  <label for="obs">Observações:</label>
  <textarea type="text" class="form-control" name="obs" placeholder="Observações" required></textarea>
 </div>

</div>



   <button type="submit" class="btn btn-primary">Enviar</button>

  </form>

Remove the @php tag below the form, then in router.php删除表单下方的@php 标签,然后在router.php

Route::post('/sugestoes', 'PostController@store')->name('sugestoes');

Then in Controller:然后在控制器中:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{

public function store(Request $request)
{
    $name = $request->input('obs');

   return redirect('/sugestoes'); // you should have GET in Route.php


    //
  }
}

Add the following code in your action attribute on the form.在表单的 action 属性中添加以下代码。 It will capture the post URL.它将捕获帖子 URL。 When you submit the form it will send the form data to the URL end-point.当您提交表单时,它会将表单数据发送到 URL 端点。

action="{{ url('sugestoes')}}"

Then die and dump in your controller store function然后死并转储到您的控制器存储功能中

public function store(Request $request)
{
    dd($request->all());

}

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

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