Form clears parameter from given action url - PHP Laravel - php

<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

Related

How to shift _token to the last of the url in laravel

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.

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

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 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 foreach loop not outputting to form

Here is a form from my Laravel 5 project:
<form class="form-group" action="/counselors/{{ $counselor->id }}/badges/add" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
#foreach($badges as $badge)
<div class="input-group">
<span class="input-group-addon">
<input type="checkbox" style="float: left" value="{{ $badge->id }}"> {{ $badge->name }}
</span>
</div>
#endforeach
<br>
<input type="submit" class="form-control btn btn-primary" name="submit" value="Submit"><hr>
<input type="button" class="form-control btn btn-danger" name="cancel" value="Cancel" onClick="location='/counselors'">
</form>
The $badges is a collection of badges from a database. They all iterate and display properly, but when i look at the request data with
$request->all()
it returns:
{"_token":"5E4csIJU4YVVQZSoPG1EmpfHfjNjYcRwuOoreCcE","submit":"Submit"}
It doesn't include the checkbox data from the loop. Is this incorrect? What's the best way to do this? There are 160 items in the $badges collection.
I'm pretty new so please go easy.
put name attribute to the input checkbox and giv it a value
<input type="checkbox" name="check-{{ $badge->id }}" style="float: left" value="{{ $badge->id }}"> {{ $badge->name }}
Note :
here i use the name "check-" and the id of the badge to get defferent name for each checkbox

Categories