Reputation: 23
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.
Upvotes: 0
Views: 85
Reputation: 2636
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
Upvotes: 2
Reputation: 448
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.
Upvotes: 0