Pressing Submit Button on Post Form Only Reloads the Page - php

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.

Related

How to pass correct data from a dynamic button in Laravel 5.8

I have a dynamic buttons where it produced from my database
My code for that in blade file
<div class="input-group col-sm-12">
<!--start of the form-->
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<!--input type hidden department code below -->
#foreach($departments as $department)
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<!--buttons -->
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
#endforeach
</form>
<!--end-->
</div>
Whenever I click either one, they'all add a data in my call database based on the values per button. My problem is when I click the cashier button, the values that would add would be the assessments.
My code in CallController
public function store(Request $request)
{
$dept_id = Department::select('id')
->where('dept_name', $request->input('dept_name'))
->first();
$let = Department::select('letter')
->where('dept_name', $request->input('dept_name'))
->first();
$number = Department::select('start')
->where('id', $dept_id->id)
->first();
$call = Call::create([
'dept_id' => $dept_id->id,
'letter' => $let->letter,
'number' => $number->start,
'called' => $request->input('called')
]);
Department::where('id', $dept_id->id)
->increment('start');
return redirect()->route('call.index')->with('success' , 'NEW CALL');
}
I also dd each query and found out that the values would get are the values from assessment or the last value from the foreach loop in my blade file. How could I get the value of cashier when I click the cashier button instead of assessment.
I would show my database so that you understand my question
Department Table: id, dept_name, letter, start(int, it'll increment after producing a call)
Counter: id, counter_num, dept_id
Call Table: id, dept_id, letter, number, counter_id, called
When you click either button, they will submit their parent form. Because both are under the same form, the data from the first button will be submitted. You will have to make a separate form for each button in order to have them submit their own data.
<div class="input-group col-sm-12">
#foreach($departments as $department)
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
</form>
#endforeach
</div>
You're creating multiple submit buttons in the same form.
Add the tag inside the loop so you have a unique form per case.
When there's multiple submit buttons inside the same form, the loop will finish and only the last results will be mapped to the buttons, thus is will submit the last information in your loop.
#foreach($departments as $department)
<form class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<input type="hidden" id="dept_name" name="dept_name" value="{{ $department->dept_name }}">
<input type="hidden" id="called" name="called" value="NO">
<!--buttons -->
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add">
{{ $department->dept_name }}
</button>
</form>
#endforeach

Form clears parameter from given action url - PHP Laravel

<form role="search" method="get" action="{{ route('people.search', array_replace(Request::all(), [" type" => "customers"])) }}">
<div class="input-group">
<input type="text" name="search" class="form-control" placeholder="Search...">
<div class="input-group-btn">
<button class="btn btn-primary"><i class="fa fa-search"></i></button>
</div>
</div>
</form>
The following form action link looks like this:
http://127.0.0.1:8080/people/search/customers?countries=Germany&statuses=3&page=1
But when I submit the search form, it redirects to:
http://127.0.0.1:8080/people/search/customers?search=test
Instead of including the parameters that were previously set.
Is there a way to prevent that?
What I did to include the previous parameter is just by adding them as hidden fields:
#foreach(request()->query() as $key => $value)
#if($key == "page") // Don't want the page parameter to be in there
#continue
#endif
<input type="hidden" name="{{$key}}" value="{{$value}}">
#endforeach

My PHP submit button does not work. How to solve it?

I am using Laravel 5 and I have some forms in one page. All is works except the last one. It is nothing happen, no error. I tried to put alert in the button. The alert showed, but the data won't saved.
<tr>
<div>
<form action="{{ url('AddComment') }}" method="POST">
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<button id ="af" class="btn btn-round btn-success">Submit</button>
</td>
</div>
</form>
</div>
</tr>
You need to have one submit button to submit a form until you submit it using jquery.
<tr>
<div>
<form action="{{ url('AddComment') }}" method="POST">
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="submit" value="Submit"> // This is SUBMIT button.
</td>
</div>
</form>
</div>
</tr>
Or,
If you want to keep the <button> as it is, then you should use jquery/JS function to submit it.
add button type too
<button id ="af" class="btn btn-round btn-success" type="submit">Submit</button>
You are not sending CSRF token in the form. Please use CSRF token in your form.
{ csrf_field() }}
Then you need to use
<input type="submit"/>
for submitting form.If you want to use
<button/>
to sbumit form,then you have to submit your form using jquery. Your form should look like.
<form action="{{ url('AddComment') }}" method="POST">
{ csrf_field() }}
<div>
<td>
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="submit" value="Submit">
</td>
</div>
</form>
Check these points:
1. Add { csrf_field() }} into your form
2. Change button to input type submit (<input type="submit"/>)
3. Check your modal and find out if all fields are fillable.
4. Print your query and check what sql query has been created.
Add CSRF:
<input type="hidden" value="{{csrf_token()}}" name="_token" id="token">
OR
{!! csrf_field() !!}
Good luck
Thank you for all the answer to helping me. I have tried it all but still nothing happen. But I looked at #JYoThI comment which said 'You can't place the form as a child element of table ,tbody, tr .' then I moved the form tags inside the <td> and the it is work !!
<td>
<form action="{{ url('AddComment') }}" method="POST">
<textarea class="form-control edit" id="com_comment2" name="com_comment2" style="width:90%" placeholder="Type a New Comment.." required=""></textarea>
<input type="hidden" name="com_leads2" value="{{ $leads[0]->LED_CODE }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
<button id ="afjk" class="btn btn-round btn-success">Submit</button>
</form>
</td>
Check #af on your JavaScript, maybe you put preventDefault() on it. Because if not, every posted answer should have solved your problem by now

Laravel delete item from table list / db

I am trying to delete item from a generated table of items which are from a database table.
My Route:
Route::delete('destroy/{deviceID}', ['as' => 'destroyDevice', 'uses' => 'DeviceController#destroyDevice']);
My Controller method to delete an item:
public function destroyDevice(Request $request, $deviceId = 0)
{
$device = Device::find($deviceId);
if($device)
{
$device->delete();
return redirect()->route('index')->with('success', 'Erfolgreich gelöscht');
}
else
{
return redirect()->route('index')->with('error', 'Fehler');
}
}
And my blade template:
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<td>
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</td>
</form>
If I click on the button nothing happens no error no Response, what am I doing wrong.
If I click on the third delete button the form holds this:
<form action="http://localhost/app/public/device/destroy/3" method="post" name="delete_device"></form>
You can solve this by putting the form inside a td tag in that table.
Like this:
<td> <!-- <--- put these -->
<form action="{{ route('destroyDevice', $deviceValue->id) }}" method="post" name="delete_device">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="id" value="{{ $deviceValue->id }}">
<button type="submit" class="btn btn-danger" name="destroy_device">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>
</td> <!-- <--- put these -->
I think the form gets ignored somehow due to not being valid, but I am not 100% sure. May people edit this answer ;)
The parameter is case sensitive so it should be deviceID instead of deviceId
public function destroyDevice(Request $request, $deviceID = 0)
Maybe you have some script that prevents the form to submit, some prevent default maybe on button click or on form submit. Check that.

Laravel 5.1 form hidden _method does not work

I'm having issues with getting a laravel app to update or delete a resource.
Here is my view.
#extends('admin.master')
#section('content')
<h1>Create an Article</h1>
<form action="/articles/{{ $article->id }}">
<input type="hidden" name="_method" value="PUT">
{!! csrf_field() !!}
#include('admin.partials.forms.article')
<div class="row">
<button type="submit" class="btn btn-success btn-lg">Update Article</button>
</div>
</form>
#endsection
Here is my controller
public function update($id, Request $request)
{
return "Update Article Code Here!";
}
All I get when I submit the form is a blank page with the url
app.dev/articles/1?_method=PUT&_token=LL6Z5zHNUG1dLjjH2TDpXXCWbGnfiCKTY4cuoVbm&title=Our+Upcoming+Event+Now+Updated&description=a+brief+event+description&body=Updated+Body&category=Events
The issues is that while you have to have the hidden method to allow laravel to see what you're doing, you also have to have the method="POST".
<form action="/articles/{{ $article->id }}" method="POST">
<input type="hidden" name="_method" value="PUT">

Categories