laravel: form action isn't working in laravel-5.8 - laravel

When i click on submit button to submit data then it says:
404|not found
route
Route::resource('contactform', 'ContactformController');
blade
<form class="leave-comment" action="{{ route('contactform.store') }}" method="POST">
<h4 class="m-text26 p-b-36 p-t-15">
Send us your message
</h4>
<div class="bo4 of-hidden size15 m-b-20">
<input class="sizefull s-text7 p-l-22 p-r-22" type="text" name="contact_name" placeholder="Full Name">
</div>
<div class="bo4 of-hidden size15 m-b-20">
<input class="sizefull s-text7 p-l-22 p-r-22" type="text" name="contact_email" placeholder="Email Address">
</div>
<textarea class="dis-block s-text7 size20 bo4 p-l-22 p-r-22 p-t-13 m-b-20" name="contact_message" placeholder="Message"></textarea>
<div class="w-size25">
<!-- Button -->
<button type="submit" class="flex-c-m size2 bg1 bo-rad-23 hov1 m-text3 trans-0-4">
Send
</button>
</div>
</form>
controller
public function store(Request $request)
{
$request->validate([
'contact_name' => 'required',
'contact_email' => 'required|email',
'contact_message' => 'required'
]);
// send email
Mail::to('test#test.com')->send(new ContactformMail());
}

Anytime you define an HTML form in your application, you should include a hidden CSRF token field in the form so that the CSRF protection middleware can validate the request. You may use the #csrf Blade directive to generate the token field:
<form method="POST" action="/profile">
#csrf
...
</form>

Related

How to add form action to laravel function

I have a website I am currently editing tht was built with laravel. I have have a page that displays a "details of shipped package"
I added a form to page to update the current location of the shipped package on the details page.
<div class="row mb-30">
<div class="col-lg-12 mt-2">
<div class="card border--dark">
<h5 class="card-header bg--dark">#lang('Courier Location')</h5>
<div class="card-body">
<form action="{{route('....')}}" method="POST">
#csrf
<div class="modal-body">
<div class="form-group">
<label for="current_location" class="form-control-label font-weight-bold">#lang('Current Location')</label>
<input type="text" class="form-control form-control-lg" name="current_location" value="{{__($courierInfo->current_location)}}" required="">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn--primary"><i class="fa fa-fw fa-paper-plane"></i>#lang('Update')</button>
</div>
</form>
</div>
</div>
</div>
I have also added the update function in the controller
public function courierUpdate(Request $request, $id)
{
$request->validate([
'current_location' => 'required',
]);
$courierInfoUpdate =CourierInfo::findOrFail($id);
$courierInfoUpdate->current_location = $request->current_location;
$courierInfoUpdate->save();
$notify[] = ['success', 'Courier location info has been updated'];
return back()->withNotify($notify);
}
I am having problem with the laravel route to call that should be added as form action.
Declare a route on the web.php
Route::post('/courier-Update/{id}','App\Http\Controllers\YourControllerName#courierUpdate')->name('courier.Update');
and now just call this route in your form and also pass the id of that courier
like this:
route('courier.Update',$courier->id)
You can add a new route in routes/web.php
//import your controller at Beginning of the file
use App\Http\Controllers\YourController;
Route::post('update_location/{id}', [YourController::class, 'courierUpdate'])->name('updateLocation');
//or
Route::post('update_location/{id}', 'YourController#courierUpdate')->name('updateLocation');
And then in your blade view
<form action="{{ route('updateLocation', [ 'id' => $id]) }}" method="POST">
#csrf
</form>

How i will get current page title in hidden field, when i submit the form in laravel

I have a form which have name, email and phone. But i want current page title or url when user submit the form, Because I want to track the user page. Please let me know How i can do it.
This is my form code.
<form class="callus" method="post" action="{{url('/sendmailuser')}}">
#csrf
<div class="col-md-3 col-sm-6">
<div class="single-query form-group">
<input type="hidden" name="product_title" value="{{ request('request') }}" class="keyword-input" placeholder="Enter Your Name" required="">
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="single-query form-group">
<input type="text" name="user_name" class="keyword-input" placeholder="Enter Your Name" required="">
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="single-query form-group">
<input type="text" name="user_email" class="keyword-input" placeholder="Enter Your Email" required="">
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="single-query form-group">
<input type="text" name="user_phone" class="keyword-input" placeholder="Enter Your Phone" required="Field is required">
</div>
</div>
<div class="col-md-3 col-sm-6">
<div class="single-query form-group">
<input type="submit" value="submit now" class="btn-blue">
</div>
</div>
</form>
This is my Email send code...
public function sendyourmail(Request $r)
{
$validator = Validator::make($r->all(), [
'user_name' => 'required',
'user_email' =>'required',
'user_phone' =>'required',
'user_desc' =>'required',
]);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator);
}
$data=[
'user_email' => $r->user_email,
'user_name' => $r->user_name,
'user_phone' => $r->user_phone,
'user_desc' => $r->user_desc,
];
Mail::send('mail', $data, function($message) use($data) {
$message->from($data['user_email'], 'Email Form Sumit');
$message->to('sumit#gmail.com')->cc('sumit#sumit.com');
$message->subject($data['user_desc']);
});
return redirect()->back()->with('message','Your Mail Has Been sent Successfully, We Will Get Back to You Shortly.');
}
You should define the title of the rendered page within the controller and past it as parameter to the view when you specify which template should be rendered.
public function sendyourmailform()
{
$title = 'Sample Page Title';
return view('email_template.blade.php', compact('title'));
}
Because the title variable is past to the template you can access it inside the template by rendering it twice inside the title section
#section('title')
{{ $title }}
#endsection
Inside the layout page which is extends by the template which render the form you should have define section named title like this
<head>
<title>
#section('title')
#endsection
<title>
</head>
And you have access to the title variable inside the template part which show the related form.
<form class="callus" method="post" action="{{url('/sendmailuser')}}">
#csrf
<div>
<input type="hidden" name="page_title" value="{{ $title }}">
</div>
<!-- ... -->
</form>

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>

making a page and getting error: MethodNotAllowedHttpException in RouteCollection.php line 218

I am making a signup page and continuously I am getting this
MethodNotAllowedHttpException in RouteCollection.php line 218:
when I try to submit data.
what it should do, is to just submit it to the database. but it's not doing it.
welcome blade:
#extends('layouts.master')
#section('title')
Welcome!!
#endsection
#section('content')
<div class="row">
<div class="col-md-6">
<h2>Sign Up</h2>
<form action="{{route('signup')}}" method="post">
<div class="form-group">
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="name">Name</label>
<input class="form-control" type="text" name="name" id="name">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{Session::token()}}">
</form>
</div>
<div class="col-md-6">
<h2>Sign In</h2>
<form action="#" method="post">
<div class="form-group">
<label for="email">Your E-Mail</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="password">Password</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
#endsection
User controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class UserController extends Controller
{
public function postSignUp(Request $request)
{
$email = $request['email'];
$name = $request['name'];
$password = bcrypt($request['password']);
$user = new User();
$user -> email = $email;
$user -> name = $name;
$user -> password = $password;
$user->save();
return redirect()->back();
}
routes.php:
Route::group(['middleware' => ['web']], function(){
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup',[
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
});
Header blade:
<header>
<nav class="navbar navbar-default">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-bsexample-navbar-collapse-1" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#"></a>
</div>
</div><!-- /.container-fluid -->
master blade:
<!DOCTYPE html>
<html>
<head>
<title>#yield('title')</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
#include('includes.header')
<div class="container">
<div>
#yield('content')
</div>
</div>
</body>
</html>
You forgot to specify route name
route('signup')
Route::post('/signup', 'UserController#postSignUp')->name('signup');
If your route like this then action should be '/signup' like this
Route::post('/signup',[
'uses' => 'UserController#postSignUp',
'as' => 'signup'
]);
This Exectiption you are facing
either because you dont have that signup route in routes.php
OR
you are doing post but you have written route::get('signup')
So you need to write route::post there.
Since you are using PATCH in your form, you have to add a method field helper to spoof the PUT HTTP verb.
Add this in your form or use post as the verb in routes and form.
{{ method_field('PUT') }}
We have following HTTP methods - get, post, put, patch etc.
When you are trying to insert a record in the database table, we need to use POST method instead of the PATCH method in HTML form.

How to upload picture and store to database in Laravel 5

I need to upload picture and store to database in Laravel 5.
My current code is:
Form:
<form method="POST" enctype="multipart/form-data" action="{{ url('products/new) }}">
{!! csrf_field() !!}
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" id="name" class="form-control" required>
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
Controller:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|max:100|unique:products',
]);
$input = $request->all();
Product::create($input);
return redirect('products');
}
The function $request->hasFile('image')) returns false.
hasFile('image') returns false because there is not an input with name 'image', only the id is 'image'
You should give it a name property, since that's the way the inputs are sent through http.
Instead of this:
<input type="file" id="image">
use this
<input name="image" type="file" id="image">
and in your validator use this:
$this->validate($request, [
'image' => 'required|max:100|unique:products',
]);

Resources