Laravel 8 Form Submit does not submit, unexpected behavior occurs - php

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" ...

Related

Why data isn't storing with controller#store method

I used method store from a controller and data doesn't store in my database.
I want to insert NOM_ARTICLE, PHOTO_ARTICLE, TYPE, DESCRIPTION_ARTICLE in three tables from a form in native HTML, but I don't know what action to do.
I am using ArticleController where there is a method store and three table models.
public function create()
{
return view('addarticle');
}
public function store(Request $request)
{
$article = new article;
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->LABEL_TYPE = $request->LABEL_TYPE;
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
Here are my tables from database:
article ('ID_ARTICLE','NOM_ARTICLE','ID_TYPE,'DESCRIPTION_ARTICLE')
photo_articles('ID_PHOTO','ID_ARTICLE','PHOTO_ARTICLE')
type('ID_TYPE','TYPE')
My HTML form:
<form method="post" action="" class="contact_form text-center" id="contact_form">
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000" />
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required" />
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE"placeholder="Description" required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
And I have my route in web.php:
Route::resource('addarticle','ArticleController');
And this is my Article model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class article extends Model {
public $table = 'article';
public $primaryKey ='ID_ARTICLE';
return $this->belongsTo('photo_articles');
}
After I click on the submit button it shows a URL like this: http://localhost/testprojet/public/addarticle?NOM_ARTICLE=test&ID_TYPE=book&MAX_FILE_SIZE=250000&PHOTO_ARTICLE=villle+icon.jpg&DESCRIPTION_ARTICLE=ss
And redirecting to my view addarticle but nothing gets added in the database.
Yaa You clearly missed csrf token to pass with the form. If you want to bubmit any form in Laravel, you should pass csrf token.
use #csrf and blade will pass the unique token to the form submit
https://laravel.com/docs/5.8/csrf
Example:
<form method="post" action="" class="contact_form text-center" id="contact_form">
#csrf
<div class="">
<div class="col-lg-4">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article" required="required">
</div>
<br/>
<div class="col-lg-4">
<input type="text" class="contact_input" name="ID_TYPE" placeholder="Type d'article" required="required">
</div>
<div>
<input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
<input type="file" class="contact_input" name="PHOTO_ARTICLE" placeholder="Capture de votre article" name="fic" size=50 required="required"/>
<!-- <input type="submit" value="Envoyer" /> -->
</div>
</div>
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE" placeholder="Description"
required="required"></textarea>
<button class="contact_button" type="submit">Valider!</button>
</form>
What I see from your form is that you didn't put any action in it, you just left it empty:
<form method="post" action="" class="contact_form text-center" id="contact_form">
You should be putting in a route to your store method in your controller, so that when the form is submitted, the data will be passed into your controller.
Also, providing a link to your localhost isn't helping us to understand your problem as we can't access to it. A solid screenshot will do.
You should put the action according to the resource pattern.
Your web.php
Route::resource('articles','ArticleController');
Your form:
<form method="post" action="{{route('articles.store')}}" class="contact_form text-center" id="contact_form">
#csrf
You should include the #csrf blade directive, as well.
In you controller:
public function store(Request $request)
{
$article = new Article();
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->save();
return redirect()->route('addarticle');
}
You're adding the PHOTO_ARTICLE to the article but you dont have this column in your articles table as you said, so it dont make sense to use the line below:
$article->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
Also, you're not receiving any LABEL_TYPE from your request, and you dont have this column in your articles table too. So you must remove this line:
$article->LABEL_TYPE = $request->LABEL_TYPE;
Try this: I hope it will help you.
Controller
public function store(Request $request)
{
$articleObj = new article;
$articleObj->add($request);
//second table
$employerObj = new Employer();
$employerObj->add($request);
}
Create three model according to your datatables and paste this code for all.
function add($request)
{
$this->NOM_ARTICLE = $request->NOM_ARTICLE;
$this->LABEL_TYPE = $request->LABEL_TYPE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->PHOTO_ARTICLE = $request->PHOTO_ARTICLE;
$this->save();
return $this->id;
}

Laravel - Edit and Update Page

I am using Laravel and I am trying to create an edit page and call my update method on submit, the problem is I am getting a 404 when updating. This is my blade file for editing like so:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="PUT" action="/admin/professions-update/{{ $data->pkprofession }}">
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Here are my routes:
Route::get('/admin/professions-edit/{id}', 'v1\ProfessionsController#edit');
Route::put('/admin/professions-update/{id}', 'v1\ProfessionsController#update');
And Here are the methods being called:
public function edit($id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
public function update(Request $request, $id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
Why am I getting a 404 error and how do I fix it?
Thanks,
In laravel docs, HTML forms do not support PUT, PATCH or DELETE
actions. So, when defining PUT, PATCH or DELETE routes that are called
from an HTML form, you will need to add a hidden _method field to the
form. The value sent with the _method field will be used as the HTTP
request method:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
You may use the #method Blade directive to generate the _method input:
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
There are so many issues in your code lets resolve one by one:
action="/admin/professions-update/{{ $data->pkprofession }}">
change it to:
action="{{ url('/admin/professions-update/' . $data->pkprofession) }}">
and then HTML forms do not support PUT, PATCH or DELETE actions, so chage it to:
<form action="{{ url('/admin/professions-update/' . $data->pkprofession) }}" method="POST">
#method('PUT')
#csrf // this is required when you are using the method other then 'get'
other elements
</form>
You're missing the csrf token and the method input. Try this:
#extends('adminlte::page')
#section('title', 'AdminLTE')
#section('content_header')
<h1>Professions</h1>
#stop
#section('content')
<form method="POST" action="/admin/professions-update/{{ $data->pkprofession }}">
#csrf
#method('PUT')
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
#stop
Also, in your update method you are forgeting to update the object, add this to your code:
$data->update($request->all());
For more info: DOCS

Pressing Submit Button on Post Form Only Reloads the Page

I need to post values from an HTML form but every time I press the submit button the page reloads, and that's it. I've checked the routes and controller, and everything seems fine to me.
Blade
<div class="panel-body">
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
#foreach($users as $users)
#if(session("admin")==0)
Meno: {{$users["name"]}} Email: {{$users["email"]}}
Registrovaný: {{$users["created_at"]}}
#endif
#if(session("admin")==1 AND $users["admin"]==0)
<form action="/promote" method="POST">
Meno: {{$users["name"]}} Email: {{$users["email"]}}
Registrovaný: {{$users["created_at"]}}
<input type="hidden" name="id" value="{{$users["id"]}}">
<button type="submit" class="w3-button w3-green">Promote</button>
</form>#endif
#if(session("admin")==1 AND $users["admin"]==1)
<form action="/demote" method="POST">
Meno: {{$users["name"]}} Email: {{$users["email"]}}
Registrovaný: {{$users["created_at"]}}
<input type="hidden" name="id" value="{{$users["id"]}}">
<button type="submit" class="w3-button w3-red">Demote</button>
</form>#endif
<br>
#endforeach
</div>
Routes
Route::post('/promote', 'users_controller#promote')->middleware('auth');
Route::post('/demote', 'users_controller#demote')->middleware('auth');
Controller
public function promote(Request $req)
{
$id = $req->input('id');
DB::table('users')->where("id", $id)->update(["admin" => 1]);
return redirect()->back();
}
public function demote(Request $req)
{
$id = $req->input('id');
DB::table('users')->where("id", $id)->update(["admin" => 0]);
return redirect()->back();
}
I want to change the database value on column "admin" on a row with the id posted in a hidden input. Now it doesn't do anything but reload the page.
You are missing the CSRF token, to solve this you should put #csrf inside your form tag, like:
<form action="/demote" method="POST">
#csrf
Meno: {{$users["name"]}} Email: {{$users["email"]}}
Registrovaný: {{$users["created_at"]}}
<input type="hidden" name="id" value="{{$users["id"]}}">
<button type="submit" class="w3-button w3-red">Demote</button>
</form>
For more info check the docs
To submit the form you would most likely need to use <input type="submit" value="Submit"> instead off <button type="submit" class="w3-button w3-red">Demote</button>.
This has caused issues for me before using plain HTML and PHP, give it a try.

my GET route not working in laravel

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');

Laravel : How to use a parameters in a Form POST to be use in a Route::post?

This my current POST route.
Route::post('/eAPI', 'ApiController#eAPI');
I wanted to make it like
Route::post('/q={$number}', 'ApiController#eAPI');
But in my form.
<form action="{{url('/eAPI')}}" method="post" id="search">
<div class="form-group">
<label for="number" class="col-md-4 control-label">Telephone Number to search :</label>
<div class="col-md-6">
<input class="form-control" id="number" name="number" placeholder="Phone (eg. 5551234567)" required>
</div>
</div>
<div class="col-md-2">
<input type="submit" name="name" value="Find" class="btn btn-success">
</div>
</form>
Now, I want to put a variable in this part, something like this.
<form action="{{url('/?q=$number')}}" method="post" id="search">
In post request you should do it like this:
Route::post('/eAPI/{q}', 'ApiController#eAPI')->name('my_route');
And in HTML Form:
<form action="{{ route('my_route', ['q' => '4']) }}" method="post" id="search">
</form>
And inside controller you can retrieve it as:
Class ApiController {
public function eAPI($q) {
// Use $q here ...
}
}
Hope this helps!
This works for me [method post and url has ?q=someValue] :
public function eApi(Request $request){
$q = $request['q'];
}
This code will get all params in post and get method
$request->all()
Hope it helps!
I never did and will never do this with post requests, but it works with get requests:
$q = request()->q;
And you don't need to add this to the route: q={$number}, just add parameters to url: ?q=value1&s=value2&c=value3

Categories