MethodNotAllowedHttpException in laravel 5.4 - laravel-5.4

View page
<form action="{{url('/deleteUser/'.$value->id)}}" method="POST" onclick="return confirm('Are you sure to delete?')">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<input type="submit" value="Delete" class="btn btn-danger">
Route
Route::get('/deleteUser/{id}','UserController#destroy');
this is my error

Change method="POST"into method="GET" or Route::getinto Route::post

Route::post('/deleteUser/{id}','UserController#destroy');

Related

How to send a post request that the user clicked on 1 of the buttons, and tell the server which one

The task is as follows. The user (Admin) checks the entries for correctness, and then clicks the accept or reject button. There are several such records (3, but I don't think it matters), each record has 2 such buttons. I tried to do something like
<form action="/admin" method="POST">
<input type="button" name="button" value="accept" class="admin-buttons">
</form>
<form action="/admin" method="POST">
<input type="button" name="button" value="decline" class="admin-buttons">
</form>
and then on server
app.post('/admin', checkAuthenticated, (req, res) => {
if (req.body.button.value == 'accept'){} // changes on db
}
But it didn't work, so here I am.
Found the error
The code below should work
app.post('/admin', checkAuthenticated, (req, res) => {
if (req.body.button == 'accept'){} // changes on db
}
req.body.button.value will give you undefined as its taking button to be an object
Next time console.log(req.body) for better debugging
And for your forms
<form action="/admin" method="POST">
<input type="text" name="button" value="accept" class="admin-buttons" hidden="">
<input type="submit" name="button" value="accept" class="admin-buttons">
</form>
<form action="/admin" method="POST">
<input type="text" name="button" value="decline" class="admin-buttons" hidden="">
<input type="submit" name="button" value="decline" class="admin-buttons">
</form>

What does <form action = ""> mean ? Many videos I saw assign the action attribute an empty string and I have no idea what it means

<form method="POST" action="">
{% csrf_token %}
{{ form.as_p }}
<input class="button" type="submit" value="Submit">
</form>
What will happen after I click the Submit button?
Syntax
<form action="URL">
the URL shows Where to send the form-data when the form is submitted.
Example
On submit, send the form-data to a file named "process_input.php" (to process the input):
<form action="/process_input.php" method="get">
<input type="text" id="name" name="name">
</form>
read more: https://www.w3schools.com/tags/att_form_action.asp

Form sending GET request to Flask when POST is expected

I have a form in my Flask template.
<form id="answer_form" action=http://127.0.0.1:5000/answer_doubt method="POST">
Answer:<textarea rows='10' cols="100" form="answer_form" wrap="soft" name="answer"></textarea>
<input type="hidden" value="{{ entry.content.id }}" name="id">
<button type="submit">Submit</button>
</form>
The method of the form is clearly declared POST but it is sending GET.
Therefore, I am unable to use request.form.
What should I do to solve this error. Am I making, like, a really obvious mistake?
Thanks in advance!
Remove the full url from your action.
<form id="answer_form" action="/answer_doubt" method="POST">
Answer:<textarea rows='10' cols="100" form="answer_form" wrap="soft" name="answer"></textarea>
<input type="hidden" value="{{ entry.content.id }}" name="id">
<button type="submit">Submit</button>
</form>

How connect a Switch Button with a form

I have a web application and I want to implement in it a Switch Button using this bootstrap example, so that the user can choose the language. Also, I need it to be submitted with a POST.
How can I code this type of toggle button, e.g.,
<div class="btn-group btn-toggle">
<button class="btn btn-xs btn-default">pt</button>
<button class="btn btn-xs btn-primary active">eng</button>
</div>
so that when the user clicks in one of them, a POST is submitted with "pt" or "eng" values?
EDIT: I just need a simple POST with redirect. I'm using Django and to translate documention suggests the use of this code:
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}">
<select name="language">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="Go">
</form>
but I don't want a dropdown and the "Go" button. I would prefer the switcher.
You can create a bootstrap form as follows. I have assumed you want to validate the form using a php script (send.php). Add the name attribute, which will help you fetch the values in the validation script.
<form class="form-horizontal" action="send.php" method="post">
<div class="btn-group btn-toggle">
<button class="btn btn-xs btn-default" name="pt">pt</button>
<button class="btn btn-xs btn-primary name="eng" active">eng</button>
</div>
</form>
Give the buttons names and values:
<button class="btn btn-xs btn-default" name="language" value="pt">pt</button>
<button class="btn btn-xs btn-primary active" name="language" value="eng">eng</button>
Then read the data just like you would from your <select>.
Sending only language option with a POST (through AJAX)
// only if pressed a non-active button
$("button", "#language-selection").click(function (event) {
var $btn = $(event.currentTarget)
// only if not already active
if (!$btn.hasClass('active')) {
// you can switch the button as soon as clicked or in AJAX callback
changeActiveButton($btn)
$.post('https://jsonplaceholder.typicode.com/todos/1', {lang: $btn.text()}, function (data) {
// do whatever you want with your API callback data
})
}
})
function changeActiveButton($btn) {
// reset active & button type on previous button
$("button.active", "#language-selection")
.removeClass('btn-primary active')
.addClass('btn-default')
// set active & button type on current button
$btn
.removeClass('btn-default')
.addClass('btn-primary active')
}
#import "https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<div id="language-selection" class="btn-group btn-toggle">
<button class="btn btn-xs btn-default">pt</button>
<button class="btn btn-xs btn-primary active">eng</button>
</div>

Post request with multiples buttons to different functions in the same controller

I have a blade view. When the user select an item a modal view appears in the same HTML.
My modal have 3 buttons. Each button have to redirect by post request to specific functions in my controller.
CarsController
class CarsControllerextends Controller
{
index(){
...
return view('cars')->with(['car'=>$response]);
}
// execute when the user click on save button
save(Request $request){
...
}
// execute when the user click on delete button
delete(Request $request){
...
}
// execute when the user click on remove button
remove(Request $request){
...
}
}
Modal
<form role="form" method="POST" action="{{ url('/guardarTurno') }}">
{{ csrf_field() }}
<!-- 3 hidden inputs to save the id of the item that the user selected ... is the best way?-->
<input type="hidden" id="cancha" name="cancha" value="" class="form-control">
<input type="hidden" id="fecha" name="fecha" value="{{$agenda['fechaElegida']}}" class="form-control">
<input type="hidden" id="hora" name="hora" value="" class="form-control">
<button type="submit" class="btn btn-labeled btn-success button-infousuario">
<span class="btn-label"><i class="fa fa-check fa-fw"></i></span>
Save
</button>
<button type="submit" class="btn btn-labeled btn-warning button-infousuario">
<span class="btn-label"><i class="fa fa-exclamation-triangle fa-fw"></i></span>
Delete
</button>
<button type="submit" class="btn btn-labeled btn-danger button-infousuario">
<span class="btn-label"><i class="fa fa-times fa-fw"></i></span>
Remove
</button>
</form>
What I want is:
Save button make a post request to save() method
Remove button make a post request to remove() method
Delete button make a post request to delete() method
Each of three button need the same ids
Thanks!
You will have to make 3 different forms to send to 3 different functions.
After your Save button, close the first </form>.
Then start a new form for each of the remaining buttons (Delete & Remove). Each form will have a different action
<form action="/remove" method="post">
{{ csrf_field() }}
<input type='hidden' name='id' value='1' />
<button type='submit'>Remove</button>
</form>
Then the same for Delete
Don't forget to define routes for each of the action=''