I am new to laravel and working on a form.
This is the form
<form action="/product" method="GET">
<div class="input-group">
<input type="text" class="form-control" placeholder="Enter product name" />
<div class="input-group-btn">
<input type="submit" class="btn btn-danger" value="Search" />
</div>
</div>
And this is the Route I have written
Route::get('/product/{product}', 'FlashCartController#find_product');
When I submit my form it says
NotFoundHttpException in RouteCollection.php line 161:
How do I submit this form?
In this case you need product ID in the form action. For example
<form action="/product/{{$productId}}" method="GET">
If you just want to create new product then lose the {product} and change the GET to POST as forms are submitted with mostly with POST.
<form action="/product" method="POST">
Route::post('/product', 'FlashCartController#find_product');
Related
This question already has answers here:
Error 405 (Method Not Allowed) Laravel 5
(9 answers)
Closed 1 year ago.
Why do I keep getting this error?
Route::post('/dashboard/project/create/project/1', [DashboardController::class, 'create'])->name('project.create');
form:
<div class="border-color col-md-10">
<div class="profile">
<form action="/sender" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
First name: <input type="text" name="fname"><br>
<input type="text" name="content"><br>
<input type="submit">
</form>
</div>
</div>
link to create
Create Project
Your form (action="/sender") is not submitting to the route you posted.
<a href="{{route('project.create')}}"> opens the route, but using GET.
Use the route name in your form's action to send your form as POST.
<div class="border-color col-md-10">
<div class="profile">
<form action="{{route('project.create')}}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
First name: <input type="text" name="fname"><br>
<input type="text" name="content"><br>
<input type="submit">
</form>
</div>
</div>
I am trying to create a form submit but I bumped to a unexpected scenario.
When form submitted by clicking the submit button
The web app reroute to my index page of the resource route
With the form variable and values appended in the URL.
Example
URL before submitting: http://127.0.0.1/admin/products/create
URL after submitted: http://127.0.0.1/admin/products?_token=qQ4klvK2egdsP77iMY4RQhXd5laJDUONRyuh8oQd&productTitle=&productPrice=
View (create.blade.php)
<form type="POST" name="productAddForm" action="{{ route('products.store') }}" >
#csrf
<div class="mb-3 col-5">
<label for="productTitle" class="form-label">Title</label>
<input name="productTitle" type="text" class="form-control" id="productTitle">
</div>
<div class="mb-3 col-5">
<label for="productPrice" class="form-label">Price</label>
<input name="productPrice" type="number" class="form-control" id="productPrice">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
Controller (ProductController.php)
public function create()
{
return view('layouts/admin/product.create');
}
public function store(Request $request)
{
dd($request->all());
}
Route (web.php)
Route::resource('admin/products', ProductController::class)->middleware('auth');
The URL parameters in your second link are a giveaway that your form data is being serialized as a GET request instead of submitted as a POST request.
There is no <form> type= attribute. You need to use method=.
<form method="POST" ...
Hello I want to shift the _token in the last of URL asshown below
What I am getting:
example.com/search?_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q&query=php
What I want:
example.com/search?query=php&_token=qkPc5aNyEp7tysbyQhZcnjHdP1wi9q
My Form code Is like
<form action="search" method="GET">
{!! csrf_field() !!}
<div class="main-search-input fl-wrap">
<div class="main-search-input-item">
<input type="text" name="query" value="" placeholder="Search snippets..." required>
</div>
<button class="main-search-button" type="submit">Search</button>
</div>
</form>
Put the csrf field at the bottom of the form tho I think you don't need it.
I have installed the latest laravel. I Have made this simple form. I want to create post but when I submit it goes to localhost/post which is the wrong URL . The actual URL is http://localhost/laravel_practice/'
Form
<form method="post" action="/post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter Title Here">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control" placeholder="Enter the body"></textarea>
</div>
<div class="form-group">
<input type="submit" name="sumit" class="btn btn-primary" value="Publish">
</div>
My Routes
Route::get('/' ,'PostController#index');
Route::get('/posts/create', 'PostController#create');
Route::post('/post','PostController#store');
Your short fix is to use action="/laravel_practice/post" or action="/laravel_practice/public/post" depending on what url you want to go.
However, it is a bad practice. You should use route name. To do that give any name to the route like below,
Route::post('/post','PostController#store')->name('post.store');
Then in view you should use,
<form method="post" action="{{ route('post.store') }}">
To read more about named route you can go through this document.
I have a form in Laravel5
<form method="POST" action="http://localhost:8000/song/Baby/update" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PATCH">
<input name="_token" type="hidden" value="kagIHsGe3zOZSPVyW6wW84Cn5eresZ2nlF287nNK">
<div class="form-group">
<input class="form-control" name="title" type="text" value="Baby">
</div>
<div class="form-group">
<textarea class="form-control" name="lyrics" cols="50" rows="10">
Yo Yo Yo BABY
</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update Song">
</div>
</form>
Then in Route file I have written the code
patch('songs/Baby/update','SongsController#update');
Its throwing error
Sorry, the page you are looking for could not be found.
NotFoundHttpException in RouteCollection.php line 143:
Is the syntax changed for PATCH request in Laravel 5?
Your route and form action are different.
You have defined a route with songs (plural) and used as song (singular) in form action.
Try changing your form action to
action="http://localhost:8000/songs/Baby/update"
Try this: <input type="hidden" name="_method" value="PUT"> and Route::put('songs/Baby/update','SongsController#update').