How to set a route to a controller and access it via a form? [duplicate] - laravel

This question already has answers here:
Laravel form post to controller
(2 answers)
Closed 3 years ago.
I'm trying to set a route to my "MessagesController" so that a i can access it from my form, How do i set a route to the controller and access it from action form ?
This is my Form code
<form action="#" id="ajax-contact" method="GET" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
What should i write inside my web.php to set a route so i can acces my MessageController.php what should i write inside the action attribute ?

Okay not sure what you want but here is the whole thing
REQUEST(FORM) => ROUTE => CONTROLLER => RESPONSE
So you want to go from FORM to Controller you have to :
Create a controller (in App\Http\Controllers) and a method
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class MyController extends Controller
{
// This method will handle the form
// $request is where the form data will be
public function handleForm(Request $request){
// this means dump all form values
dd($request->all());
}
}
Create a route ( In simple words, this will link between FORM & Controller method ) in web.php
// The request need to be POST to /handle-data
Route::post('handle-data','MyController#handleForm');
finally this should be your form
<form action="/handle-data" id="ajax-contact" method="POST" enctype="multipart/form-data" >
{{csrf_field()}}
<div class="input-field">
<input type="text" class="form-control" name="name" id="nom" required
placeholder="Nom">
</div>
<div class="input-field">
<input type="email" class="form-control" name="email" id="email" required
placeholder="E-mail">
</div>
<div class="input-field">
<textarea class="form-control" name="message" id="message" required
placeholder="Message"></textarea>
</div>
<button class="btn" type="submit">Soumettre</button>
</form>
If you want to make this as an ajax call
$( "#ajax-contact" ).submit(function( event ) {
event.preventDefault();
var data = $(this).serializeArray();
$.post('/handle-data', data ).then(function(response){
console.log(response);
});
});
if you want to test the ajax way, change the dd($request->all()); to return response()->json($request->all());
That should be it :)

Edit routes inside web.php to point to submit method in MessagesController:
Route::post('submit', 'MessagesController#submit')->name('submit');
then set form action to:
<form action="{{ route('submit') }}" id="ajax-contact" method="POST" enctype="multipart/form-data">
You should POST from a form.

Related

Change PUT request to POST request to include image update

I wrote a post edit method which at first consisted only of text. I did the update using a PUT request.
Now I want to include images to my posts, so I added it, and it works for my POST request of creating a post but doesn't work for my update post, when I want to change image, since PUT request doesn't support file uploads.
So now I'm stuck trying to change my update method from PUT request that only updates text to a POST request that updates both the text and an image if supplied.
This is the code I wrote so far:
public function update(Request $request, Post $post)
{
//store updated image
if($request->hasFile('image') && $request->file('image')->isValid()){
if($post->hasMedia('posts')) {
$post->media()->delete();
}
$post->addMediaFromRequest('image')->toMediaCollection('post', 's3');
}
$post->update(request()->validate([
'body' => 'required'
]));
return redirect($post->path());
}
I think that the $post->update doesn't work for the POST request. I just want to update a text if an update was given.
Using Laravel 6.
EDIT: My form layout structure (simplified)
<form action="action="/posts/{post}" method="POST">
#method('PUT')
#csrf
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>
My routes:
Route::get('/posts/{post}/edit', 'PostsController#edit')->name('posts.edit');
Route::put('/posts/{post}', 'PostsController#update');
I tried your code and it worked fine with me , only added #csrf in form tag.
<form action="/posts/{post}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group row">
<input id="body" type="text" class="form-control" name="body" value="{{ old('body', $post->body) }}">
<input id="image" type="file" class="form-control" name="image">
<button type="submit" class="btn btn-primary">Update Post</button>
</form>

The GET method is not supported for this route. Supported methods: POST in laravel ...is there something i'm missing?

blade file :
<form action="{{URL::to('/admin/sender')}}"method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Controller:
public function notificationSender(Request $request)
{
$text= request()->text;
print_r($request->input());
event(new OrderComplete($text));
return view('admin.sender');
}
Route:
Route::post('/sender','HomeController#notificationSender');
the route is a subroute for a group..is there something i'm missing?
Give space between method and route parameters here
if your laravel version is 5 then use this
<form action="{{URL::to('/admin/sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
Or else you can also name the route to make it easy to pass in action parameter of form like this.
Route::post('/sender','HomeController#notificationSender')->name('sender');
Then you can pass it in form like this
<form action="{{route('sender')}}" method="post">
{{csrf_field()}}
<input type="text" name="text">
<input type="submit">
</form>
For GET Method (Laravel 6)
blade file
<form method="GET" action="{{ route('sender') }}" enctype="multipart/form-data" >
#csrf
<input type="text" name="text">
<input type="submit">
</form>
Write Controller
public function notificationSender(Request $request)
{
$text= $request->get('text');
echo "<pre>"; print_r($text);
event(new OrderComplete($text));
return view('admin.sender');
}
Route
Route::get('sender','HomeController#notificationSender');

using route resource form did not post to store function

I have a form which upon successful validation should show the desired result. But on button press the browser displays The page has expired due to inactivity. Please refresh and try again.
view page
<form class="form-horizontal" method="POST" action="{{action('BookController#store')}}">
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>
Route code
//web.php
Route::resource('book','BookController');
controller code
class BookController extends Controller
{
public function index()
{
//
}
public function create()
{
return view('pages.book');
}
public function store(Request $request)
{
$validatedInput = $request -> validate([
'txtBookName' => 'required|string|min:3|max:100'
]);
return $validatedInput;
}
}
Form url
http://127.0.0.1:8000/book/create
on submit button press, the page is redirected to
http://127.0.0.1:8000/book and it displays The page has expired due to inactivity. Please refresh and try again.
You can either post a CSRF token in your form by calling:
{{ csrf_field() }}
Or exclude your route in app/Http/Middleware/VerifyCsrfToken.php:
protected $except = [
'your/route'
];
Implement like this to avoid Expired message.
<form action="{{ action('ContactController#store') }}" method="POST">
#csrf <!-- this is the magic line, works on laravel 8 -->
<!--input components-->
<!--input components-->
<!--input components-->
</form>
you should use {{ csrf_field() }} in your form.
Please do this after the form class, because the resource take the #method('PUT')
<form class="form-horizontal" method="POST" action="{{action('BookController#store')}}">
{{csrf_field()}}
#method('PUT')
<div class="row" style="padding-left: 1%;">
<div class="col-md-4">
<div class="form-group">
<label>Book Name</label><span class="required">*</span>
<input type="text" maxlength="100" minlength="3" required="required" runat="server" id="txtBookName" class="form-control" autocomplete="off" autofocus="autofocus" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</form>

submit form can not access method in controller

When I submit form in view, I can't access that in controller.
View create.blade.php
#extends('layouts.app')
#section('content')
<form method="POST" action="posts">
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
Controller PostController
public function store(Request $request)
{
return $request->all();
}
I want to access the form data, in PostController in the store method and once I click submit, it not giving the result.
I have route: Route::resource('posts', 'PostController') and I am posting to this route/url, as action="posts". In Route I have used resource and in method="post"
Does it matter? What can I do?
Looks like you are missing the action
Route::post('post', 'PostController#store');
I'm not sure but may be it's because of url and not included csrf_field(). Try changing it to:
<form method="POST" action="{{ url('posts') }}">
{{ csrf_field() }}
<input type="text" name="title" placeholder="Enter title">
<input type="submit" name="submit">
</form>
or you may use route('posts.store ') instead of url('posts')

post method show error as soon ass change post into get working better in laravel routing

here is my route
Route::post('/posts', 'Cdesigination#index');
HTNL and input Field
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="name" id="name" data-validate="required" placeholder="Enter Name" />
</div>
<div class="form-group">
<label class="control-label">Detail</label>
<textarea class="form-control" name="detail" id="detail" placeholder="Enter Detail"></textarea>
</div>
<input type="hidden" id="_token" name="_token" value="{{ csrf_token() }}">
<div class="form-group col-sm-offset-3">
<button type="submit" onclick="postdata();" id="post" class="btn btn-success">Submit Now</button>
</div>
my controller file
<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\models\Designation as desig;
class Cdesigination extends Controller
{
public function index()
{
return view('designation');
}
}
where is the error have trouble me get method working fine but post method show error as soon as change
this route show following error
Route::post('/posts', 'Cdesigination#index');
MethodNotAllowedHttpException in RouteCollection.php line 219:
as soon as change post to get like Route::get('/posts', 'Cdesigination#index');
error not show working fine tell me how solve it
Make sure your <form> has method="" that matches your route declaration
<form method="post">
for
Route::post('/posts', 'Cdesigination#index');
Or
<form method="get">
for
Route::get('/posts', 'Cdesigination#index');

Resources