I have two submit buttons within a form, and I need to detect which one is used to trigger a certain action. Unfortunately, my check isn't detecting which submit button was clicked:
Form:
<button type="submit" name="complete">
<span class="btn-label">
<i class="glyphicon glyphicon-save"></i>
</span> Submit Audit
</button>
<button type="submit" name="pending">
<span class="btn-label">
<i class="glyphicon glyphicon-save"></i>
</span> Save Audit
</button>
Controller:
public function updateAudit(Request $request, $id)
{
....
if ($request->has('complete')) {
$auditCompleteCheck->completed = 1;
$auditCompleteCheck->save();
}
}
If I output $request, I can see the complete submit button as: "complete" => "", and similarly with pending: "pending" => "".
But, if I dd($request->has('complete'));, it always returns false, even though I can see it in the request bag.
Why isn't the ->has check working?
Many thanks.
Try setting the same value for the attribute name for both button elements and a different value for the attribute value.
HTML part:
<button type="submit" name="submit_audit" value="complete">
<span class="btn-label">
<i class="glyphicon glyphicon-save"></i>
</span> Submit Audit
</button>
<button type="submit" name="submit_audit" value="pending">
<span class="btn-label">
<i class="glyphicon glyphicon-save"></i>
</span> Save Audit
</button>
Now in your controller, you can do the following:
if ($request->submit_audit === 'complete') {
// Do something
}
if ($request->submit_audit === 'pending') {
// Do something else
}
Related
I am facing problem for Submit Button not getting correct route to add new data.
2 Blade file with 2 Submit Button which need to save 2 data to 2 different tables.
Problem facing - Data submitted in 1 table for both submit button....
addnewBuffalo.blade.php
<div class="box-header with-border" align="right">
<button type="submit" class="btn btn-success btn"><i class="fa fa-floppy-o"></i> <b>Add
Buffalo</b></button><a href="" class="btn btn-warning btn"><i class="fa fa-refresh">
</i> <b> Refresh</b></a>
</div>
addnewcow.blade.php
<div class="box-header with-border" align="right">
<button type="submit" class="btn btn-success btn"><i class="fa fa-floppy-o"></i> <b>Add
Cow</b></button><a href="" class="btn btn-warning btn"><i class="fa fa-refresh"></i> <b>
Refresh</b></a>
</div>
web.php
Route:: post('submit', 'App\Http\Controllers\addnewbuffaloController#addbuffaloData')
->name('submit');
Route:: post('submit', 'App\Http\Controllers\addnewcowController#addcowData')
->name('submit');
You need to create 2 separate Route Names. Having ->name('submit') for both won't work. And having the same path won't work
Route::post('submit=buffalo', 'App\Http\Controllers\addnewbuffaloController#addbuffaloData')
->name('submit-buffalo');
Route::post('submit-cow', 'App\Http\Controllers\addnewcowController#addcowData')
->name('submit-cow');
Or if the form is wrapped by one form tag then you can handle both submissions in the one Controller action
<button type="submit" name="buffalo-submission">Save Buffalo</button>
<button type="submit" name="cow-submission">Save Cow</button>
Then in the controller action code you could check
if ($request->has('buffalo-submission') {
Do buffalo code
} else if ($request->has('cow-submission')) {
do cow code
}
I have an error when showing the data I do not see the problem because the data does not want to show it I hope you can help me.
this my event click on table its work display my modal
<td>
<a href="#" wire:click.prevent="edit({{$pais->id}})" data-toggle="modal" data-target="#update_pais">
<i class="fa fa-search text-warning"></i>
</a>
</td>
This is my component function edit
public $testNew;
public function edit(Pais $pais)
{
$this->testNew = '324234234234';
}
this my input
<input wire:model="testNew" type="text">
when click data doesn't show and input is empty on network show data is correct
Have you tried adding a value attribute to your input?
<input wire:model="testNew" type="text" value="{{ $testNew }}">
my database does not update while when i do
dd(request()->has('validated'));
this is what my web.php looks like:
Route::patch('prof/theme/{id}/validated', 'Theme\ThemeController#valide')->name('prof.theme.validated');
this is what my ThemeController.php looks like:
public function valide(Theme $theme) {
$theme->update([
'validated' => request()->has('validated')
]);
return back();
}
this is what my show.blade.php looks like:
<form action="{{route('prof.theme.validated', $theme)}}" method="POST">
#csrf
#method('PATCH')
#if ($theme->validated)
<button type="submit" class="btn btn-danger text-center" style="width: 350px">
INVALIDER LE THEME
</button>
#else
<button type="submit" class="btn btn-success text-center" name="validated" id="validated" style="width: 350px">
VALIDER LE THEME
</button>
#endif
</form>
this code does not show me any error but it does not produce the expected action. my variable "validated" is a boolean
thank you Harshith VA!!!!! i was able to solve the problem thanks to your idea. I kept your view and used your controller idea this way
public function valide(Request $request, Theme $theme) {
$theme->validated = $request->has('validated');
$theme->save();
return back();
}
thanks you for your help
From first look, I would believe this is due to the fact that you have added name="validated" to the <button> and not as an <input>.
<form action="{{route('prof.theme.validated', $theme)}}" method="POST">
#csrf
#method('PATCH')
#if ($theme->validated)
<button type="submit" class="btn btn-danger text-center" style="width: 350px">
INVALIDER LE THEME
</button>
#else
<input type="hidden" name="validated" value="true">
<button type="submit" class="btn btn-success text-center" style="width: 350px">
VALIDER LE THEME
</button>
#endif
</form>
Here, we add a hidden input field with the validated input. Doing this should pass that the validated key/value to the controller.
Another point. dd() is used to die and dump data, and would stop execution of whatever script is running. Therefore it won't save to the db once you do this.
The above assumes you didn't mean it doesn't save to the database when you dd().
Update
Also note that ->has() checks if the input exists. You should use request('validated') to get its value.
After clicking on DELETE, there is no reaction, absolutely nothing happens. I am using HTML Forms, that's why I used, #method('DELETE'). I had similar problem with edit, after using #method(PUT), edit is working properly.
<div class="row">
<div class="col-6 align-self-start">
<a class="btn btn-light" href="/listings/{{$listing->id}}/edit">Edit</a>
</div>
<div class="col-6 align-self-end">
<form action="{{action('ListingsController#destroy', [$listing->id])}}" method="POST">
<?= csrf_field()?>
#method('DELETE')
<a class="btn btn-danger" href="/dashboard">Delete</a>
</form>
</div>
</div>
Destroy Function:
public function destroy($id)
{
$listing = Listing::find($id);
$listing->delete();
return redirect('/dashboard')->with('success', 'Contact Deleted');
}
Change
<a class="btn btn-danger" href="/dashboard">Delete</a>
to
<a type="submit" class="btn btn-danger" href="/dashboard">Delete</a>
You should add submit otherwise it will not submit the form.
Its recommended to use a instead of :
<button type="submit" class="btn btn-danger">Delete</button>
I'm building a sample app in Laravel based around tenancies. Landlords can add tenants, but it's not an accept/request system. Once the Landlord clicks add and fills out a form, the tenancy is created.
This is a sample row in the database.
Database row.
I added the accepted method so a tenant can accept/reject, but it's just sitting there atm, it's not doing anything (yet). How can I allow the 'add' to be a request, and for the landlord to accept or reject it?
This is the profile page. I was thinking of having the logic like this. How hard is it?
<div class="container">
#if($user->userType != "Landlord")
<div class="row">
`//Add Tenancy - I'll start this so only lanlors can see this button`
`Start Tenancy`
`//If Tenancy request sent`
`button to accept or reject.`
`//If if in tenacny`
`list property address, or landlord name`
</div>
You can create a form around your Add Tenancy button and when the button is clicked the form is submitted and you're redirected where you want to go. This is how the default Laravel Auth example works when logging out.
<form method="POST" action="/account/tenancy/{{$user->id}}/create">
<button type="submit">Add Tenancy</button>
</form>
You can handle the request via routers/controller.
Edit
If you want to control the button based on the status of the tenancy you can pass a $status variable to your blade template and use it.
#if ( $status == 'can-add' )
<button type="submit" class="btn btn-primary">Add Tenancy</button>
#elseif ( $status == 'accepted' )
<button type="submit" class="btn btn-success" disabled>Accepted</button>
#elseif ( $status == 'denied' )
<button type="submit" class="btn btn-danger" disabled>Denied</button>
#elseif ( $status == 'pending' )
<button type="submit" class="btn btn-warning" disabled>Pending</button>
#else
<button type="submit" class="btn btn-default" disabled>A Default</button>
#endif