I am building a website with Laravel 5.2 and I did a form in layout blade:
<form action="POST" action="{{url('subscribe')}}" class="searchform">
{!! csrf_field() !!}
<input type="email" placeholder="Your email address" name="email" />
<button type="submit" class="btn btn-default" ><i class="fa fa-arrow-circle-o-right"></i></button>
<p>Get the most recent updates from <br />our site and be updated your self...</p>
</form>
And this is my route
Route::post('/subscribe', 'Front#subscrib');
And this function in the controller Front
public function subscrib()
{
echo"we in subsc function";
if (Request::isMethod('post')) {
Subscribe::create([
'email' => Request::get('email')
]);
}
}
But when I write email in the form, I get this error:
NotFoundHttpException in RouteCollection.php line 161:
in RouteCollection.php line 161
at RouteCollection->match(object(Request)) in Router.php line 823
at Router->findRoute(object(Request)) in Router.php line 691
And URL go to
http://localhost/larashop/public/POST?_token=ALqduK9gW6Xdnq9iOJzyu7kMji1z3LCXDgeQehoO&email=azharnabil%40yahoo.com
Why does this happen?
You've set your form's action - where it sends the data - to POST (the second action attribute is being ignored as a result).
You should be setting the method to POST.
<form action="POST" action="{{url('subscribe')}}" class="searchform">
should be:
<form method="POST" action="{{url('subscribe')}}" class="searchform">
Related
I am using laravel 7 and keep having this issue in my form:
<form class="form-inline " role="form" method="POST" action="{{route('auth.signup')}}">
<input type="hidden" name="_token" value="{{csrf_token()}}">
<div class="form-group">
<input type="email" class="form-control" id="inputEmail" name="email" value="email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password1" name="password">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password2" name="password_confirmation">
</div>
<button type="submit" class="btn btn-primary mx-auto">Sign Up</button>
</form>
i already have changed config/session.php This line 'lifetime' => env('SESSION_LIFETIME', 1200),
Is it necessary to show route / controller they all just basic
I have also tried to remove the controller's entire code and simply dd("signed up") but i keep getting the same issues
I also went on to change my php.ini file but the problem persists.
maybe another piece of information to add is that the homepage which has the signup link to the form above also has its own form with a and its not the same token as the one in the form above
The sign up controller:
public function postSignup(Request $request)
{
$this->validate($request,[
'email'=>'required|unique:appscheduler_users|email|max:255',
'name'=>'required|max:255',
'password'=>'required|confirmed|min:6',
]);
$user=user::create([
'email'=>$request->input('email'),
'name'=>$request->input('name'),
'password'=>bcrypt($request->input('password')),
'created'=>date('Y-m-d H:i:s'),
'ip'=>$request->ip(),
]);
Auth::login($user);
if(Session::has('oldUrl')) {
$oldUrl=Session::get('oldUrl');
Session::forget('oldUrl');
return redirect()->to($oldUrl);
}
return redirect()
->route('home')
->with('info','Your Account has been created');
}
To solve this error you first need to insert one of the following commands into the form tag.
#csrf OR {{ csrf_field }}
If your problem is not resolved, do the following: and keep the csrf tag
1.Insert one of the following commands into the form tag #csrf OR {{ csrf_field }}
2.Open the .env file and change the values to the "file" in the SESSION_DRIVER section.
3.Then you should reset laravel cache. type below commands in the terminal
php artisan view:clear php artisan route:clear php artisan cache:clear
php artisan config:cache
4.In the final step, unplug the project from the serve and click again on php artisan serve
I hope your problem is resolved
maybe you wanna take a look here
Post request in Laravel - Error - 419 Sorry, your session has expired
Im having trouble posting my form to my database using laravel. When i click submit, it shows me the error MethodNotAllowedHttpException in RouteCollection.php line 218. My HTML code is shown below. I have defined the routes as shown below and I have also pasted my PostController which contains the store function.
<div class="blog-page blog-content-2">
<div class="row">
<div class="col-lg-9">
<div class="blog-single-content bordered blog-container">
<div class="blog-comments">
<h3 class="sbold blog-comments-title">Leave A Comment</h3>
<form method="post" action="store">
<div class="form-group">
<input name="title" type="text" placeholder="Your Name" class="form-control c-square"> </div>
<div class="form-group">
<textarea name="body" rows="8" name="message" placeholder="Write comment here ..." class="form-control c-square"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn blue uppercase btn-md sbold btn-block">Submit</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
This is my route page
Route::resource('posts', 'PostController');
This is the PostController which contains the store function which is suppose to store the data into the database.
public function store(Request $request)
{
//Validate the data
$this->Validate($request, array(
'title'=>'required|max:255',
'body'=>'required'
));
//Store the data into the database
$post = new Post;
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->save();
//redirect to another page
return redirect()->route('posts.show', $post->id);
}
The problem is here:
<form method="post" action="store">
You should put posts here:
<form method="post" action="posts">
You can see all routes created with Route::resource() by using php artisan route:list command. Here, you need to look at URI created for posts.store route.
Also, you need to add CSRF token to your form:
<form method="post" action="posts">
{{ csrf_field() }}
<form method="post" action="store"> will send you to the path store which you do not have, your form should post to the same url, like this:
<form method="post" action=".">
use
<form method="post" action="posts">
#foreach($subscription as $subscriptionData)
<div class="col-lg-4 col-md-4 col-sm-6">
<!--whitebox-->
<div class="whitebox">
<div class="cntent">
<form name="frmPackage{{$subscriptionData['id']}}" method="post" action="{{url('payment')}}">
<h2>GBP {{$subscriptionData['subscription_amount']}} Membership Fee/month for {{$subscriptionData['free_for_month']}} free + additional {{$subscriptionData['subscription_applicable_for']}} pitches</h2>
<!-- Buy Now -->
<input type="hidden" name="_token" value="{{csrf_token()}}">
<input type="hidden" name="package_id" value="{{$subscriptionData['id']}}">
<input type="submit" name="btnSubmit{{$subscriptionData['id']}}" value="Buy Now">
</form>
</div>
</div>
<!--whitebox-->
</div>
#endforeach
Route file
Route::group(['middleware' => 'web'], function () {
Route::resource('payment', 'PaymentController');
});
In that code I have generate 3 form and when I click on any form my PaymentController's store method will call. But it fire error.
TokenMismatchException in /var/www/html/vonitto/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php line 67:
I got a solution. If you don't want csrf_token for a selected URL.
Then got to app => Http => Middleware and fine VerifyCsrfToken.php
file. In this file there is one variable that is $except that holds
those URL in which you don't require a csrf token. So add your URL
like this.
protected $except = [
'payment/*',
];
What is causing this exception? What is the meaning of MethodNotAllowedException? I am using Laravel 5.2.
HTML and input fields:
<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>
AJAX function:
function postdata(){
var name=$('#name').val();
var detail=$('#detail').val();
var token=$('#_token').val();
$.ajax({
type: 'POST',
url: '{{url("/posts")}}',
data: "name="+ name + "&detail="+ detail+"&_token="+ token ,
success: function(data){
} });
}
Route:
Route::post('/posts', 'Cdesigination#index');
Error after clicking button:
MethodNotAllowedHttpException in RouteCollection.php line 219:
in RouteCollection.php line 219
at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 206
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158
at RouteCollection->match(object(Request)) in Router.php line 823
at Router->findRoute(object(Request)) in Router.php line 691
check the routes.php file, what method you are using for this request, it should be something like:
Route::post('/posts', 'AnyController#method');
<form id="login-form" action="brand-dashboard" method="post">
<span class"email">email</span>
<input type="email" name="email">
<span class"email">password</span>
<input type="password" name="password"><br><br>
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
<input type="submit" value="login">
</form>
this is in my view page..then in my route..
Route::get('/brand-dashboard','BrandsController#auth_brand_admin');
in my Brands controller..i use the method like
public function auth_brand_admin()
{
return ('sample text');
}
but i got error when submiting the form ..the error is..
MethodNotAllowedHttpException in RouteCollection.php
Change your code to this
Route::post('/brand-dashboard','BrandsController#auth_brand_admin');
It's because you register route with GET method but send POST request.