Laravel Submit button re-routing to Home Page - php

So I used the default Registration Form Laravel.
Made copy of the Registration Form to make a slightly different one.
Basically there is two sign up forms
Starter - original
Bronze - Copy
--My Routing List--
Auth::routes();
Route::get('/register', function(){
return View::make('auth.register');
});
Route::get('/bronze', function(){
return View::make('auth.bronze');
});
Route::get('/login', function(){
return View::make('auth.login');
});
Now every time I submit the bronze sign up form, it redirects me to my home page.
I'm thinking maybe its my Action call in my view. which is identical to the Starter Form
<form class="form-horizontal" role="form" method="POST" action="{{ url('/register') }}">
if I'm correct what I just did is bad practice, but I'm a new-bie at this framework.

You need to explicitly add a POST route, as the Auth::routes() method only handles calls to /register:
Route::post('/bronze', 'Auth\RegisterController#register');
And make sure you submit the Bronze form to the same route:
<form class="form-horizontal" role="form" method="POST" action="/bronze">
Alternatively, and possibly a better solution, would be to just use the same registration form and have an input for their account type selection:
<form class="form-horizontal" role="form" method="POST" action="/register">
<input type="radio" name="type" id="type-starter" value="starter">
<label for="type-starter">Starter</label>
<input type="radio" name="type" id="type-bronze" value="bronze">
<label for="type-bronze">Bronze</label>

Old question but had a similar issue.
Don't forget to include csrf token in all your forms if you have that middleware enabled.
These days it's as simple as calling a blade helper:
<form action="..." method="...">
#csrf
...
</form>

Related

How to use different form with different controller functions and the same route in Laravel

I'm building an app with Laravel
In the same blade file I have different forms. In a div I have two forms.
In one form the user writes something in a textarea. After press the button ok, the page is reloaded and the data stored in a table of database.
The other form appears in a modal and it contains other option that the user can set and after press another button the data should stored in another table of database.
So how I can do this? I tried to route the function in my controller in this way:
Route::post('/anam','AnamnController#store');
Route::post('/anam','AnamnController#storePar');
and the tag form:
<form action="{{ action('AnamController#store') }}" method="post" class="form-horizontal">
<form id="formA" action="{{ action('AnamnesiController#storePar') }}" method="" class="form-horizontal">
But these return to me errors. So what I have to do?
Thanks!
it's impossible to use the same route for two different functions unless you change one by get instead of post for example:
Route::post('/anam','AnamnController#store');
Route::get('/anam','AnamnController#storePar');
but you can make this logic:
Route::post('/anam','AnamnController#store');
and the tag form:
<form action="{{ action('AnamController#store') }}" method="post" class="form-horizontal">
<input name="input_name" value="input-val" />
</form>
in controller:
public function store (){
if(request()->input_name == 'val-1')
this->store1();
else
this->store2();
}
protected function store1 () {
//
}
protected function store2 () {
//
}
You can't have two routes with the same url that has the same route method.
In your case you have two POST methods going to the same url. The second route definition is cancelling out the first.
Rename one of them.

419 unknown status in form post request

I am a beginner in laravel framework. Now I am creating a form which will send post request to /abc.php. However, after submitting the form, error unknown server error with status 419 is reported.
I have googled about this issue and I figured out that it was caused by csrf_token. I tried to except verify csrf token in this route and forms were submitted successfully.
Therefore, I have added {{ csrf_field() }} after the <form>tag and submit the form again but the form submit failed. Except not verifying the csrf token in my form, what can cause this problem? Thank you very much!
My route
Route::post('/abc.php','formSubmitController#submit');
My form
<form class="myform" name="myform" id="myform" method="post" action="/abc.php" onsubmit="return validation();" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
....
</form>
try so...
Route::post('/abc','formSubmitController#submit')->name('abc');
<form class="myform" method="post" action="{{route('abc')}}" onsubmit="return validation();" enctype="multipart/form-data">
#csrf
....
</form>

Laravel my post method (store) form not working

PostsController.php
public function store(Request $request)
{
Post::create($request->all());
return redirect('/posts');
}
my view create.blade.php in posts directory
#extends('layout.app')
#section('content')
<h1>Create Post</h1>
<form action="/posts" method="post">
<input type="text" name="title" placeholder="Enter title"> <!-- this name=title comes from create_posts_table -->
{{--{{csrf_field()}}--}}
<input type="submit" name="submit">
</form>
#stop
Route
Route::resource('/posts','PostsController');
when I submit browser goes to localhost/posts and it says:
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.33 (Win32) OpenSSL/1.1.0h PHP/7.2.5
and no record wont save
try :
<form action="{{ route('posts.store') }}" method="post">
//
</form>
or you can read this document for more information. and take note you CAN NOT send post request by browser calling url. you can submit a form for it
There are two things wrong with this.
You're using a resource route so you need to route appropriately to the store method:
<form action="{{ route('posts.store') }}" method="post">
Also, you need to spoof methods on your other controller endpoints such as the DELETE method as it will fail with a POST request you can do that like so:
<form action="{{ route('posts.delete') }}" method="post">
#csrf
{{ method_field('DELETE') }}
Take a look here I cover just that on this video https://youtu.be/kPYtJo7RyUQ

How is update done in Laravel 5.4

I'm still learning Laravel and I'm working with version 5.4. I'm currently trying to perform an update and want to see the contents of the request using dd, but I get redirected to the view page (strange). I compared the documentation and I seem to be doing right. Below is the captured url when i submit the update form
http://127.0.0.1:8000/tasks/2?_token=gX4bBZoZ0bpMgeQ5uIbLNrIegohvAOUJmPTNjbX0&_method=PUT&employee_id=Harry+Ovie&title=update&description=Testing+task&priority=high&begin=2017%2F06%2F02&end=2017%2F06%2F05
This is my route list
Route::get('/tasks', 'TaskController#index');
Route::get('/tasks/create', 'TaskController#create');
Route::post('/tasks', 'TaskController#store');
Route::get('/tasks/{id}', 'TaskController#show');
Route::get('/tasks/{id}/edit', 'TaskController#edit');
Route::put('/tasks/{id}', 'TaskController#update');
This is the update in my TaskController
public function update(Request $request, $id)
{
dd($request);
}
And this is what my form looks like
<form class="form-horizontal" role="form" action='/tasks/{{$task->id}}'>
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
How do i fix my code?
Try posting your form, right now you're performing a GET.
<form class="form-horizontal" role="form" method="post" action='/tasks/{{$task->id}}'>
HTML forms do not support the use of PUT, PATCH and DELETE (and some others).
That's why the hidden field is added and handled by Laravel to perform these actions on a POST request.

getting redirected to root when having a special character in request - laravel 5

here's my routes file:
Route::any('target',function(){
return 'got it!';
});
here's my form:
<form method='post' action='example.com/target'>
<input type='text' name='message' />
<input type='submit' name='submit' />
</form>
so It doesn't matter what I fill as the message field , It should return me got it!
but when I enter a url in message field , no matter the method is POST or GET , whether the path exist's or not, I get redirected to the root (example.com)
I don't have this problem on localhost, but on the shared host
thanks
change your action into this action='/target'
Check your APP_URL env variable in config/app.php
Make sure once you are in production (or any other env for that matter), this value is changed accordingly.
edit:
Also check your action, either use named routes and route('route-name') or the url helpers to get proper urls.
Url helpers:
<form method="post" action="{{ Url::to('/target') }}">
Named routes:
Route::any('target', function(){
return 'got it!';
})->name('target-name');
With your form action like this:
<form method="post" action="{{ route('target-name') }}">
Also, is that the only route you have specified? Or are there more routes defined where you use target ?
You can change your code in 2 ways.
First of all change the route like this
Route::get('/target', array('as'=>'target_name','uses'=>function(){
return 'got it!';
}));
After that you can use like this way
1) <form method="post" action="{{ URL::to('/target') }}">
or
2) <form method="post" action="{{ URL::route('target_name') }}">
Thanks

Categories