简体   繁体   中英

Laravel form post to controller

I am new to Laravel and I'm having trouble with posting data to a controller. I couldn't find the corresponding documentation. I want to something similar in Laravel that I do in C# MVC.

<form action="/someurl" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>

Controller

[HttpPost]
public ActionResult SomeUrl(string someName)
{
...
}

You should use route.

your .html

<form action="{{url('someurl')}}" method="post">
<input type="text" name="someName" />
<input type="submit">
</form>

in routes.php

Route::post('someurl', 'YourController@someMethod');

and finally in YourController.php

public function someMethod(Request $request)
{
   dd($request->all());  //to check all the datas dumped from the form
   //if your want to get single element,someName in this case
   $someName = $request->someName; 
}

This works best

<form action="{{url('someurl')}}" method="post">
 @csrf
<input type="text" name="someName" />
<input type="submit">
</form>

in web.php

Route::post('someurl', 'YourController@someMethod');

and in your Controller

public function someMethod(Request $request)
{
   dd($request->all());  //to check all the datas dumped from the form
   //if your want to get single element,someName in this case
   $someName = $request->someName; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM