Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
{
...
}
like image 626
Kamran G. Avatar asked Jun 03 '16 10:06

Kamran G.


People also ask

How to validate form data before submit to controller in Laravel?

And also validate form data before submit to controller using jQuery validation in laravel. First of all download or install laravel 8 new setup. So, open terminal and type the following command to install new laravel 8 app into your machine: In this step, setup database with your downloded/installed laravel 8 app.

How to allow file uploads in Laravel forms?

If your form is going to accept file uploads, add a files option to your array: Laravel provides an easy method of protecting your application from cross-site request forgeries.

What is form request validation Ajax Laravel 8?

This laravel 8 form request validation ajax tutorial will create contact us form and post or submit form data on controller using jQuery ajax. And also validate form data before submit to controller using jQuery validation in laravel.

How to create a new table in Laravel form?

So, find create_posts_table.php file inside LaravelForm/database/migrations/ directory. Then open this file and add the following code into function up () on this file: Now, open again your terminal and type the following command on cmd to create tables into your selected database:


2 Answers

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; 
}
like image 187
Sid Avatar answered Sep 19 '22 18:09

Sid


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; 
}

like image 34
Adeleye Ayodeji Avatar answered Sep 20 '22 18:09

Adeleye Ayodeji