简体   繁体   中英

Can't get input data from form LARAVEL

I'm learning Laravel and I got stuck trying to get data from a form.

I already am able to get data back with GET, but with POST I've been having a ton of trouble. 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)

First, you need to call the model, 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

@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

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. It will capture the post URL. When you submit the form it will send the form data to the URL end-point.

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

Then die and dump in your controller store function

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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