2
votes

I have a form posting a title and a body. When i submit, nothing happens. I have a csrf field and i'm using the post method.

I have tried numerous different ways to declare the action and the method. I have tried to call the csrf field in different ways also. None of that worked.

Route:

Route::resource('/posts', 'PostsController');

Create.blade.php

<form action="{{route('posts.store')}}" method="POST">
    @csrf
    // removed @method('POST')
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" name="title" class="form-control" placeholder="Title">
    </div>
    <div class="form-group">
        <label for="body">Body</label>
        <textarea name="body" placeholder="Body" class="form-control" cols="30" rows="10"></textarea>
    </div>
    <input type="button" name="submit" value="Submit" class="btn btn-primary">
</form>

PostsController:

public function store(Request $request)
    {
        return "Form posted";
    }

Post model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    // 
}

Here i have all the available routes: php artisan route:list

The store function in the PostsController is empty. So I expect to recieve an empty page when I submit, but nothing happens.

4
why do you need this: @method('POST'). Try running php artisan route:list and make sure you have a named route called posts.store. - nakov
add your controller code and model as well. - Dilip Hirapara
please add your controller code - Salman Zafar

4 Answers

1
votes

dilip hirapara gave the solution in a comment.

I removed:

<input type="button" name="submit" value="Submit" class="btn btn-primary"> 

and added:

<button type="submit" class="btn btn-primary"> Submit </button>
0
votes

Remove @method('POST')

<form action="{{route('posts.store')}}" method="POST">
     {{ csrf_field() }}
    <div class="form-group">
        <label for="title">Title</label>
        <input type="text" name="title" class="form-control" placeholder="Title">
    </div>
    <div class="form-group">
        <label for="body">Body</label>
        <textarea name="body" placeholder="Body" class="form-control" cols="30" rows="10"></textarea>
    </div>
    <button type="submit" class="btn btn-primary"> Submit  </button>
</form>

In your controller.

public function store(Request $request)
{
   echo '<pre>';
   print_r($request->all());
}
0
votes

Is this for creating a new database registry, or for editing an existing one?

If it's for creating, remove the @method('POST') bit altogether. If it's for editing, change it to @method('PATCH').

0
votes
    <meta name="csrf-token" content="{{ csrf_token() }}">

Put this in your header file. Sometime for csrf we need to put this as a meta tag.