I'm currently having an issue with posting method directly on submit buttons. What I need to do is to put two submit buttons in the same form. I've searched a little and found the html "formaction" method. The problem is that on one page, it is working just fine, but not on the other page.
So, this down here is my code that works:
<form action="" method="POST">
{{ csrf_field() }}
<input type="hidden" name="id" value="$id">
<button type="submit" formaction="{{route('remove')}}">
<i class="fa fa-check-circle" aria-hidden="true"></i>
</button>
</form>
And that one don't:
<form method="POST">
{{csrf_field()}}
<input type="hidden" name="id" value="{{$q->id}}">
<button type="button" formaction="{{route('remove')}}">
<i class="fa fa-trash" aria-hidden="true" style="font-size: 20px; color: red;"></i>
</button>
</form>
Someone has any idea why that is happening? I'm searching for this for a while now, but I just found dead ends.
suggestion
add a name="" for each button, and use $_POST["name_of_submit"] to process the form submission. example
//1st button
<button type="submit" name="submit1" formaction="{{route('remove')}}">
//2nd button
<button type="submit" name="submit2" formaction="{{route('remove')}}">
if (isset($_POST['submit1'])){
//do something
}
if (isset($_POST['submit2'])){
//do something else
}
Related
I have question on Laravel with jQuery.
Right now, I have form with three (3) different buttons (update,delete,email). Each button have different task in controller.
Normally I use button type="submit" to submit data from blade into controller.
Blade
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="submit" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="submit" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="submit" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
And in controller, I just use '$request->has()' to differentiate three request.
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
But my current goal is to show loading icon before data successfully submitted.
The reason why I want to show loading page is because system need to send an email to multiple user and it might take some time to load before it finished the task. To prevent user to click the same button multiple times.
I use jQuery click function and I change button type from 'submit' to 'button'.
But the problem is when the form submitted to controller. It doesn't recognize '$request->has()' in my controller.
Here's my html form and jQuery and my controller
blade.php
<form id='form-info' class="form-info" action="{{url('/form/sendData')}}" method="post">
#csrf
<input type="text" id="info" class="info" name="name" value="test"/>
<button type="button" class="btn btn-xs btn-warning" name="update" id="update">Update</button>
<button type="button" class="btn btn-xs btn-danger" name="delete" id="delete">Delete</button>
<button type="button" class="btn btn-xs btn-info" name="email" id="email">Email</button>
</form>
jQuery
$(document).ready(function(){
$('#update').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#delete').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
$('#email').click(function(){
$(".loader").show();
$("#form-info").submit();
$(".loader").hide();
});
});
Controller.php
if ($request->has('update'))
{
echo 'update';
}
else if($request->has('delete'))
{
echo 'delete';
}
else if($request->has('email'))
{
echo 'email';
}
How to solve this problem ? Thanks in advance.
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.
I am using modal window to post the the values to the controller .
<form action="<?php echo base_url();?>Users/sms/" id="form" method="post" class="form-horizontal" enctype="multipart/form-data">
buttons
<button type="button" id="custom" class="btn btn-warning" disabled="disabled">
Custom Msg
</button>
<button type="button" id="initial" class="btn btn-primary">
Initial
</button>
no response in my screen. i cannot able find the error. in popup window showing nothing. when i click the button no response. suggestion please
Type should be submit instead of button to post the data.
Here is my form :
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
<input type="hidden" name="_method" value="DELETE" />
</div>
</form>
Here is my controller :
public function destroy($id)
{
$invoice = Invoice::find($id);
if(!$invoice){
return redirect()->route('invoice.index')->with(['fail' => 'Page not found !']);
}
$invoice->delete();
return redirect()->route('invoice.index')->with(['success' => 'Invoice Deleted.']);
}
But it can not delete where is the problem ? How solve this ?
You need to use POST method for the form and add input element with name _method and value DELETE. Also, add token:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input name="_method" type="hidden" value="DELETE">
{{ csrf_field() }}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
I think you must add a hidden input to the form which will contain the method used:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="POST">
<input type="hidden" name="_method" value="DELETE" />
</form>
Read more on Laravel documentation about Form method spoofing
In order to get PUT and DELETE methods to work, you need an additional field, since only POST and GET are possible within HTML (out-of-the-box).
The additional field will be made with the code:
{!! method_field('DELETE') !!}
So your form will look like this:
<form action="{{ route('invoice.destroy' , $invoice->id)}}" method="DELETE">
{!! method_field('DELETE') !!}
<div class="modal-footer no-border">
<button type="button" class="btn btn-info" data-dismiss="modal">No</button>
<button type="submit" class="btn btn-primary">Yes</button>
</div>
</form>
Also, if you are using blade templates, you can add the method field like so:
#method('DELETE')
More Laravel way you can do this
<form action="{{ route('invoice.destroy',$invoice->id)}}" method="POST">
#method('DELETE')
<button type="submit" class="btn btn-primary">Yes</button>
</form>
Here is my html:
<form id="quant" method="POST" action="/portfolio" name="trans">
<div class="modal-body">
Shares : <input autocomplete="off" type="text" name="quantity">
<input type="hidden" value="<?= htmlspecialchars($_GET['q']); ?>" name="stock">
<input type="submit" name="val" value="Confirm Transaction" class="btn btn-primary">
<div id="slider-range-max"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
Here is an image of my web inspector showing that in fact that data was requested:
I have been trying to figure out why this has not been working for a long time, any suggestions would be appreciated.
The Code:
if(isset($_POST["quantity"])) {
var_dump($_POST);
which returns nothing
Then you need a slash after it to let it know it is a folder and not a file: action="/portfolio/"
If you're using something like AngularJS to submit your form, it doesn't send POST data the typical way. Check out this post.