This is my section and I want to send the form to the store function in the PostsController but when I hit submit it shows a 419 page expired however it should return to the posts page and the post should be stored in the database.
#section('content')
<form action="{{ action('PostsController#store') }}" method="POST">
<div class="form-group">
<label for="title">Title</label>
<input type="text" id="title" name="title" placeholder="Title">
</div>
<div class="form-group">
<label for="body">Body</label>
<textarea id="body" name="body" placeholder="Body"></textarea>
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
#endsection
and this is my store function in the PostsController
public function store(Request $request)
{
//create post
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts');
}
The 419 page expired error generally comes from a csrf token issue.
Try adding #csrf within your form:
<form action="{{ action('PostsController#store') }}" method="POST">
#csrf
// etc
</form>
Related
I have 2 controller with resource clients and sumsubs.
being on the clients.show route I created a form that I would like to send to the sumsubs.update endpoint but it does not work; the form still contacts sumsubs.show
here are the routes that I have defined
here is the form
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="PUT" action="{{ route('sumsubs.update', $client->id) }}">
#csrf
<div class="form-group row">
<div class="col-sm-10">
<input type="number" class="form-control" id="candidat" name="candidat" value="{{ $client->id }}" hidden required>
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-success float-right"> {{ __('Recheck') }} </button>
</div>
</form>
the following is my sumbsubs controller
public function show($id)
{
return 'not ok';
}
public function update(Request $request, $id)
{
return 'ok ';
}
this is what i get when i submit the form
I would like to know where is my mistake
I read the laravel documentation for form submission and tried with url and route methods but I still get the same result
You have to use method spoofing.
Change your method in form tag to Post:
<form accept-charset="UTF-8" class="form-horizontal" name='recheck' method="POST" action="{{ route('sumsubs.update', $client->id) }}">
and add this after #csrf:
#method('PUT')
none of solutions on net, didn't solve my problem.how can i correct this error?
form.blade.php:
<div class="row">
#include('admin.partials.errors')
<div class="col-xs-12 col-md-6">
<form action="{{ url('file') }}" name="POST" method="post" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="file_title">عنوان فایل:</label>
<input type="text" class="form-control" name="file_title" id="file_title" value="{{ old('file_title', isset($fileItem)? $fileItem->file_title:'') }}">
</div>
<div class="form-group">
<label for="file_description">توضیحات فایل:</label>
<textarea name="file_description" class="form-control" id="file_description" cols="30" rows="10" value="{{ old('file_description', isset($fileItem)? $fileItem->file_description:'') }}"></textarea>
</div>
<div class="form-group">
<label for="file_title">فایل:</label>
<input type="file" name="fileItem">
</div>
<div class="form-group">
<button class="btn btn-success" type="submit">ذخیره اطلاعات</button>
</div>
</form>
</div>
</div>
create.blade.php
#extends('layouts.admin')
#section('content')
#include('admin.file.form')
#endsection
FilesController.php
public function create()
{
return view('admin.file.create')->with('panel_title', 'ایجاد فایل جدید');
}
public function store(Request $request)
{
dd($request->all());
}
public function edit()
{
}
web.php
Route::get('/file','FilesController#index')->name('admin.file.list)//admin/user
Route::get('/file/create', 'FilesController#create')->name('admin.file.create');//admin/user
Route::post('/file/store','FilesController#store')->name('admin.file.store');//admin/file
Route::get('/file/edit/{file_id}','FilesController#edit')->name('admin.file.edit');//admin/file
Route::post('/file/edit/{file_id}','FilesController#update')->name('admin.file.update');//admin/file
});
when i click on save button to store file details on create file page in admin panel, i get this error!
i think you have to change this line
Route::post('/file/store','FilesController#store')->name('admin.file.store');//admin/file
to this
Route::post('/file','FilesController#store')->name('admin.file.store');//admin/file
Try changing
<form action="{{ url('file') }}" name="POST" method="post" enctype="multipart/form-data">
to
<form action="{{ url('file/store') }}" name="POST" method="post" enctype="multipart/form-data">
For Upload file on Laraval Public/Storage
Put this Code in Controller
FilesController.php
public function store(Request $request)
{ //after file store
return redirect(url('//**Put your Own url which you want**'));
}
Or Give another url which present in Your Web.php
When I attempt to update a record in my Laravel application, it is running the wrong URL causing an error 404. This function was working fine when I was developing locally however now it is hosted on a one.com server, it has stopped working.
edit.blade.php
<form method="POST" action="gins/{{ $gins->id }}">
#method('PATCH')
#csrf
<div class="field">
<label class="label" for="gin">Gin</label>
<div class="control">
<input type="text" class="input" name="gin"
placeholder="Gin" value="{{ $gins->gin }}">
</div>
</div>
<div class="field">
<label class="label" for="size">Bottle Size(ml)</label>
<div class="control">
<input type="text" class="input" name="size"
placeholder="Size (ml)" value="{{ $gins->size }}">
</div>
</div>
<div class="field">
<label class="label" for="price">Price(£)</label>
<div class="control">
<input type="text" class="input" name="price"
placeholder="Price of Gin" value="{{ $gins->price }}">
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-success">Update Record
</button>
</div>
</div>
</form>
Route
Route::patch('gins/{gin}', 'PostsController#update')->middleware('auth');
Auth::routes();
Controller
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
return redirect('gins');
}
The URL for the edit page is Laravel/gins/7/edit. When I click the submit button it's returning the URL Laravel/gins/7/gins/7 when it should be redirecting back to Laravel/gins/7.
The 7 in the Url is the record id from the particular record I'm attempting to update.
It's always a bad idea to hardcode urls like that. The following
<form method="POST" action="gins/{{ $gins->id }}">
in a route like laravel/gins/ would evaluate to laravel/gins/gins/7.
Also, routes change all the time in a dynamic web application. For this reason, I'd suggest you to use Named Routes.
For example:
Route::patch('gins/{gin}', 'PostsController#update')
->middleware('auth')
->name('posts.update');
and then change your form action to this:
<form method="POST" action="{{ route('posts.update', ['gin' => $gins->id]) }}">
I would also clean up your update() method a bit.
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
// change this to a named route as well
return redirect('gins');
// or if you just want to return back to the previous page, you can do
// return back();
}
I have a form which upon successful validation should show the desired result. But on button press the browser displays The page has expired due to inactivity. Please refresh and try again.
view page
<form class="form-horizontal" method="POST" action="{{action('BookController#store')}}">
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
Route code
//web.php
Route::resource('book','BookController');
controller code
class BookController extends Controller
{
public function index()
{
//
}
public function create()
{
return view('pages.book');
}
public function store(Request $request)
{
$validatedInput = $request -> validate([
'txtBookName' => 'required|string|min:3|max:100'
]);
return $validatedInput;
}
}
Form url
http://127.0.0.1:8000/book/create
on submit button press, the page is redirected to
http://127.0.0.1:8000/book and it displays The page has expired due to inactivity. Please refresh and try again.
You can either post a CSRF token in your form by calling:
{{ csrf_field() }}
Or exclude your route in app/Http/Middleware/VerifyCsrfToken.php:
protected $except = [
'your/route'
];
Implement like this to avoid Expired message.
<form action="{{ action('ContactController#store') }}" method="POST">
#csrf <!-- this is the magic line, works on laravel 8 -->
<!--input components-->
<!--input components-->
<!--input components-->
</form>
you should use {{ csrf_field() }} in your form.
Please do this after the form class, because the resource take the #method('PUT')
<form class="form-horizontal" method="POST" action="{{action('BookController#store')}}">
{{csrf_field()}}
#method('PUT')
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
Im having trouble posting my form to my database using laravel. When i click submit, it shows me the error MethodNotAllowedHttpException in RouteCollection.php line 218. My HTML code is shown below. I have defined the routes as shown below and I have also pasted my PostController which contains the store function.
<div class="blog-page blog-content-2">
<div class="row">
<div class="col-lg-9">
<div class="blog-single-content bordered blog-container">
<div class="blog-comments">
<h3 class="sbold blog-comments-title">Leave A Comment</h3>
<form method="post" action="store">
<div class="form-group">
<input name="title" type="text" placeholder="Your Name" class="form-control c-square"> </div>
<div class="form-group">
<textarea name="body" rows="8" name="message" placeholder="Write comment here ..." class="form-control c-square"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn blue uppercase btn-md sbold btn-block">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
This is my route page
Route::resource('posts', 'PostController');
This is the PostController which contains the store function which is suppose to store the data into the database.
public function store(Request $request)
{
//Validate the data
$this->Validate($request, array(
'title'=>'required|max:255',
'body'=>'required'
));
//Store the data into the database
$post = new Post;
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
//redirect to another page
return redirect()->route('posts.show', $post->id);
}
The problem is here:
<form method="post" action="store">
You should put posts here:
<form method="post" action="posts">
You can see all routes created with Route::resource() by using php artisan route:list command. Here, you need to look at URI created for posts.store route.
Also, you need to add CSRF token to your form:
<form method="post" action="posts">
{{ csrf_field() }}
<form method="post" action="store"> will send you to the path store which you do not have, your form should post to the same url, like this:
<form method="post" action=".">
use
<form method="post" action="posts">