[html] Adding form action in html in laravel

I am unable to pass url in views html form action tag.

<form method="post" action="??what to write here??" accept-charset="UTF-8">

I want to set it's action to WelcomeController@log_in function in WelcomeController file in controllers.

Here are my routes:

Route::get('/','WelcomeController@home');
Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController@log_in'));
Route::get('home', 'HomeController@index');

After submitting it keeps the same url

http://localhost:8000/

And the main error line

Whoops, looks like something went wrong.

After that there is 1/1 TokenMismatchException in compiled.php line 2440:

This question is related to html forms url laravel action

The answer is


Form Post Action :

<form method="post" action="{{url('login')}}" accept-charset="UTF-8">

Change your Route : In Routes -> Web.php

Route::post('login','WelcomeController@log_in');

For Laravel 2020. Ok, an example:

<form class="modal-content animate" action="{{ url('login_kun')  }}" method="post">
  @csrf   // !!! attention - this string is a must 
....
 </form>

And then in web.php:

Route::post("/login_kun", "LoginController@login");

And one more in new created LoginController:

 public function login(Request $request){
    dd($request->all());
}

and you are done my friend.


Your form is also missing '{{csrf_field()}}'


Laravel 5.8 Step 1: Go to the path routes/api.php add: Route::post('welcome/login', 'WelcomeController@login')->name('welcome.login'); Step2: Go to the path file view

<form method="POST" action="{{ route('welcome.login') }}">
</form>

Result html

<form method="POST" action="http://localhost/api/welcome/login">

<form>

You need to set a name to your Routes. Like this:


    Route::get('/','WelcomeController@home')->name('welcome.home');
    Route::post('/', array('as' => 'log_in', 'uses' => 'WelcomeController@log_in'))->name('welcome.log_in');
    Route::get('home', 'HomeController@index')->name('home.index');

I just put name on Routes that need this. In my case, to call from tag form at blade template. Like this:

<form action="{{ route('home.index') }}" >

Or, You can do this:

<form action="/" >

You can use the action() helper to generate an URL to your route:

<form method="post" action="{{ action('WelcomeController@log_in') }}" accept-charset="UTF-8">

Note that the Laravel 5 default installation already comes with views and controllers for the whole authentication process. Just go to /home on a fresh install and you should get redirected to a login page.

Also make sure to read the Authentication section in the docs


The error you're getting now (TokenMismatchException) is because Laravel has CSRF protection out of the box

To make use of it (and make the error go away) add a hidden field to your form:

<input name="_token" type="hidden" value="{{ csrf_token() }}"/>

Alternatively you can also disable CSRF protection by removing 'App\Http\Middleware\VerifyCsrfToken' from the $middleware array in app/Http/Kernel.php


if you want to call controller from form action that time used following code:

<form action="{{ action('SchoolController@getSchool') }}"  >

Here SchoolController is a controller name and getSchool is a method name, you must use get or post before method name which should be same as in form tag.


1) In Laravel 5 , form helper is removed .You need to first install laravel collective .

Refer link: https://laravelcollective.com/docs/5.1/html

{!! Form::open(array('route' => 'log_in')) !!}

OR

{!! Form::open(array('route' => '/')) !!}

2) For laravel 4, form helper is already there

{{ Form::open(array('url' => '/')) }}

The following should work.

{{  Form::open( array('url' => action('WelcomeController@log_in'), 'files'=>true,'method'=>'post') )  }}

...
{{ Form::close() }}

Use action="{{ action('WelcomeController@log_in') }}"

however TokenMismatchException means that you are missing a CSRF token in your form.

You can add this by using <input name="_token" type="hidden" value="{{ csrf_token() }}">


I wanted to store a post in my application, so I created a controller of posts (PostsController) with the resources included:

php artisan make:controller PostsController --resource

The controller was created with all the methods needed to do a CRUD app, then I added the following code to the web.php in the routes folder :

Route::resource('posts', 'PostsController');

I solved the form action problem by doing this:

  1. I checked my routing list by doing php artisan route:list
  2. I searched for the route name of the store method in the result table in the terminal and I found it under the name of posts.store
  3. I added this to the action attribute of my form: action="{{route('posts.store')}}" instead of action="??what to write here??"

{{ Form::open(array('action' => "WelcomeController@log_in")) }}
...
{{ Form::close() }}

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to url

What is the difference between URL parameters and query strings? Allow Access-Control-Allow-Origin header using HTML5 fetch API File URL "Not allowed to load local resource" in the Internet Browser Slack URL to open a channel from browser Getting absolute URLs using ASP.NET Core How do I load an HTTP URL with App Transport Security enabled in iOS 9? Adding form action in html in laravel React-router urls don't work when refreshing or writing manually URL for public Amazon S3 bucket How can I append a query parameter to an existing URL?

Examples related to laravel

Parameter binding on left joins with array in Laravel Query Builder Laravel 4 with Sentry 2 add user to a group on Registration Target class controller does not exist - Laravel 8 Visual Studio Code PHP Intelephense Keep Showing Not Necessary Error The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel How to fix 'Unchecked runtime.lastError: The message port closed before a response was received' chrome issue? Post request in Laravel - Error - 419 Sorry, your session/ 419 your page has expired Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required How can I run specific migration in laravel Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory

Examples related to action

Adding form action in html in laravel How to set the action for a UIBarButtonItem in Swift Execution order of events when pressing PrimeFaces p:commandButton Using Cookie in Asp.Net Mvc 4 Get controller and action name from within controller? HTML form action and onsubmit issues How to pass value from <option><select> to form action Web API Routing - api/{controller}/{action}/{id} "dysfunctions" api/{controller}/{id} Single line if statement with 2 actions How do I use two submit buttons, and differentiate between which one was used to submit the form?