Laravel - Generate route to the delete method through link - php

I have a PostController in which I have all RESTful methods. I can generate route to delete method by defining method in form tag like below,
<form action="{{route('post.destroy', [$post->id])}}" method="POST">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="submit" value="Delete">
</form>
But I need to generate same route with link,
Delete
thanks.

You have to create an link, which triggers your Form Submission with JavaScript. You cant create an Link with "POST" instead of "GET" Method.
As Basic example without JQuery and proper separation of code, add this to your template.
Add an ID to you Form "myform"
<script type="text/javascript">
function submitMyform()
{
document.myform.submit();
}
</script>
And change your link to:
Delete
if you want to show only the link, use hidden form elements.

This should do it:
Delete

Related

Can not pass the action get param to the PHP file

I have a smarty project, and in the .tpl file, there is a form:
<form method="get" action="{$smarty.server.PHP_SELF}?action=func1">
<input type="text" name="username"/>
<input type="submit">
</form>
there is a question, if the php file have many function for different action requests, so in the template if have many forms, I want to through the action for distinguish.
but in my practice, see upper code, I write like this, this can not delivery the action to my php file.
I want to write the action in the form action, because this can be more standard. so I don't want to write it in a hidden input. why write in the action can not pass into the php file?
You could use submit button with specified name and value:
<form method="get" action="{$smarty.server.PHP_SELF}">
<input type="text" name="username" />
<input type="submit" name="action" value="func1" />
</form>
and then you'll get a global variable $_POST['action'] with value func1. But the value will be showing on your button title, so I offer you to find forms only by submit name, for example name='submit_form1'.

MethodNotAllowedHttpException when using PUT and DELETE method in Laravel form

I am working on basic CRUD using Laravel. I am getting MethodNotAllowedHttpException when using PUT and DELETE method in Laravel form action. GET and POST action methods work fine.
HTML form only accept either GET or POST method so you can't use PUT and DELETE in form method. However, if you want to use PUT or DELETE then laravel provide Form method spoofing like this
<input type="hidden" name="_method" value="PUT">
Here is the form example
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
Short form
<form action="/foo/bar" method="POST">
#method('PUT')
#csrf
</form>
Route
Route::put('foo/bar', 'FooController#bar');
Check details here https://laravel.com/docs/5.6/routing#form-method-spoofing

Get user input data from form in twig file with Symfony 4

I'm trying to create a little search bar on my website with symfony 4. How do I get the the user input data from the form which can be used in the controller. The form looks like this :
//.../navbar.html.twig
<form action="search" method="get" >
<input type="text" placeholder="Search..">
<button type="submit" class="btn btn-default">Search</button>
</form>
Since you don't seem to be using a FormType and your form's method is 'GET':
First your need a name to your input. ex:
<input type="text" name="search" placeholder="Search..">
Then just pass the request service to your action in your controller and get the desired parameter.
public function yourAction(Request $request){
$searchString = $request->get('search');
}
Edit: I strongly recommend using symfony's form component tho. Doc here : https://symfony.com/doc/current/forms.html

Form submits as GET Laravel 4

I have form like this
<form action="{{ Request::root() }}/articles/update/" method="post">
<input type="hidden" name="id" value="{{ $article->id }}" />
<input type="submit" name="submit" value="Submit" />
</form>
And route like this
Route::post('articles/update', array('as' => 'articleUpdate', 'uses' => 'ArticlesController#update'));
But when I submit the form, I get MethodNotAllowedHttpException. In error report I can see that request method is GET. I have also tried using caps for method method="POST" but it didn't work.
Any ideas?
What does FireBug/Web console inspector show you? is the form being sent via GET or POST, any redirects?
Seems a redirection problem to me, after reaching the server Laravel redirects to the URL the form sent the post request.
you must use put method here. Form change like this
{{Form::open(array('url'=>'/articles/update','method' => 'PUT'))}}
Routes like this
Route::put('/articles/update','ArticlesController#update');

Javascript/HTML Form field naming issue

Consider the following simple form:
<form method="GET" action="handle.php">
<input type="hidden" name="action" value="search">
</form>
Form submission is performed by Javascript (iui) in an ajax call. All fields are properly gathered from the form. Javascript then wants to send the ajax call to "form.action".
This is where my problem starts. The object form is of type HTMLFormElement. The action property of the form is supposed to be of type string and should contain "handle.php". After some hours of debugging, I noticed that form.action is now of type HTMLInputElement.
My question:
Is this proper Javascript behavior? I would have never though that defining a form field with the name of a form attribute, this would happen. In the mean time I solved the issue by naming my field differently.
Thanks in advance for any advice...
Found an easy way of displaying my problem. First the form with the problem:
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
And the form that is proper:
<form action="test.php">
<input type="hidden" name="NOT_AN_ATTRIBUTE_NAME" value="test">
<input type="button" onclick="alert(this.form.action);">
</form>
In the first, the popup states "[object HTMLInputElement]", in the second: "http://localhost/test.php".
The issue you're seeing is because forms are special in JavaScript. All their fields are accessable as properties, so when you use this.form.action, it's getting the field action, not the HTML attribute action="test.php".
Try changing alert(this.form.action); to alert(this.form.getAttribute('action')) instead.
It seems as bug. Maybe there should be an array for 'action '
<form action="test.php">
<input type="hidden" name="action" value="test">
<input type="button" onclick="alert(this.form.action[0]);"> //the form action
<input type="button" onclick="alert(this.form.action[1]);"> // the text input
</form>
your this.form.action is still a object. instead of using alert put it in a console.log(this.form.action) and use firebug and firequery try to discover what events/properties your this.form.action has.
!you need to enable your console in firebug before you can use console.log

Categories