Laravel form post to controller - php

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

Related

laravel 8 , How to display data from a FORM with post method?

my Route :
Route::resource('/posts',PostsControllerWithAll::class);
my form :
i try to read the given data from this form
<form method="POST" action="{{ action('PostsControllerWithAll#store') }}">
#csrf
<input type="text" name="title" placeholder="enter title">
<input type="submit" name="submit">
</form>
my controller :
public function create()
{
return view("posts.create");
}
public function store(Request $request)
{
return $request->all();
}
and this is my route list
route list image , please check the link image
Try using the named route, like you show on your print screen.
<form method="POST" action="{{ action('posts.store') }}">...</form>
To validated the fields values on store method, use dd($request->all()) this dump the variable and die, it's good to see what is expected.

Laravel: Send an array through route

I was trying to send an array through a route to another view, but when i used the function get_defined_vars(), i realized that i was sending a string with the information. Is it possible to do that?
this form from my view should send the array to my route
<form action="/trans" method="POST">
#csrf
<div class="input-group">
<input type="hidden" class="form-control" name="r" value="{{$cooperado}}">
<button type="submit" class="btn btn-primary">
<span>+</span>
</button>
</span>
</div>
</form>
then this route should send the array to the other view
Route::post('/trans', function(){
$j = Input::get('r');
return view('movs.create')->with(['j'=>$j]);
});
this is the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create()
{
//
return view('movs.create');
}
}
routes.php
Route::post('/trans', 'MovimentacoesController#create');
controller
use Illuminate\Http\Request;
use App\Movimentacoes;
class MovimentacoesController extends Controller
{
public function create(Request $request)
{
$j = $request->request->get('r');
return view('movs.create')->with(['j' => $j]);
}
}
Code like this In the form tag:
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
<input type="hidden" class="form-control" name="r[]" value="{{$cooperado}}">
submit this form
then the Input::get('r') will be Array!
I hope it helps you.

Laravel 5.6 - Route not found

I am using Laravel 5.6 and trying to create a simple form to create a post. I have my web routes that looks like this..
Route::resource('posts', 'PostsController')->middleware('auth');
My form looks like this...
<form action="{{route('posts#store')}}" method="POST">
<input name="title" type="text">
</form>
And my PagesController looks like this
public function store(Request $request)
{
$post = new Post;
$title = $request->input('title');
$post->save();
}
But I am getting the following error message..
Route [posts#store] not defined
Where am I going wrong?
You may check resource controllers & try following code.
<form action="{{route('posts.store')}}" method="POST">
<input name="title" type="text">
</form>
<form action="{{route('posts.store')}}" method="POST">
<input name="title" type="text">
</form>
public function store(Request $request)
{
$post = new Post;
$post->title= $request->input('title');//change
$post->save();
}

upload file didn't work in laravel 5.1

I want to upload a .CSV file to insert some records to my Database.
Unfortunately, it doesn't work.
My Routes:
Route::get('excel/import','Backend\ExcelController#getImport');
Route::post('excel/import','Backend\ExcelController#postImport');
My Controller:
public function getImport(){
$csrf_field = csrf_field();
$postUrl='import';
$html = <<<CREATE
<form action="$postUrl" method="post">
$csrf_field
<input type="file" name="importCsv" formenctype="multipart/form-data" ><br/><br/>
<input type="submit" value="submit"/>
</form>
CREATE;
return $html;
}
public function postImport(Request $request){
//get file
$file = Input::file('importCsv');
dd($file);
$upload=$request->file('importCsv');
dd($upload);
}
It just print null if I use :
Input::file('importCsv');<br>
And nothing if I use :
$request ->file('importCsv');<br><br>
So, I've tried to print like dd($request->all()); <br>
array:2 [▼
"_token" => "wPuAXUvSItR4MFJ4bAjQhanaf0W9avrqR2PgjxcU"
"importCsv" => "T_OpusDef.csv"
]
I could get only the name, and when I want the the path by getRealPath(), It told me :
FatalErrorexception: call to a memeber function getRealPath() on null
I need your help, Thanks a lot
The form should look like this:
<form action="{{ $postUrl }}" method="post" enctype="multipart/form-data">
{{ $csrf_field }}
<input type="file" name="importCsv"><br/><br/>
<input type="submit" value="submit">
</form>
use {{csrf_field()}} instead of $csrf_field if its in blade and csrf_field() if its in controller

Laravel issue with route and HTML form action

I'm having some trouble with my form, on submit I get the error 'Whoops, looks like something went wrong.'.
I'm using Laravel 4.2, my routes look like this:
Route::get('/', function()
{
return View::make('index');
});
Route::post('/', array('as' => 'login', 'uses' => 'HomeController#login'));
And my form looks like this:
<form action="{{ action('HomeController#login') }}" method="post">
<input class="signUpField-index" id="signUpEmail-index" type="text" placeholder="Email Address (required, but never shown) *" name="email" />
<input class="signUpField-index" id="signUpPassword-index" type="password" placeholder="Password *" name="password" />
<input id="signUpSubmit-index" type="submit" value="Sign Up" />
<input name="_token" type="hidden" value="{{ csrf_token() }}"/>
</form>
And my controller looks like this:
<?php
class HomeController extends BaseController {
public function showIndex()
{
return View::make('index');
}
public function login() {
//return var_dump(_POST);
return View::make('index');
}
}
I think it might be the action that is incorrect but I am not too sure, I've tried to look at other examples and tutorials like here: Adding form action in html in laravel, but they have not helped.
Thanks in advance.
Its not action,its url.
Use:
<form url="your action" method="post">
</form>
But if you want to stay on a same page,use Ajax for submit.

Categories