Router:
Route::post('/submit/{id}', function() {
return 'Hello World';
});
HTML:
<form method="POST" action="/submit/{{$id}}">
The above changes the URL to http://127.0.0.1:8000/submit/$id and returns
Page has Expired Due to Inactivity.
It looks like Laravel is trying to force the POST into a GET.
This problem is because you forget to put the CSRF token field into the form.
Try with:
Option 1
<form method="POST" action="{{url('submit', [$id])}}">
{{ csrf_field() }}
<button type="submit">Submit</button>
</form>
Option 2
<form method="POST" action="{{url('submit')}}/{{$id}}">
#csrf
<button type="submit">Submit</button>
</form>
For more info see this link
Related
I was watching an old tutorial about laravel 7 whiles using laravel 9, i tried to create a HTML form like this.
<div class="card-body">
<form action="/upload" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>
then in my route(web.php) i added a code like this
route::post('/upload', function(Request $request)
{$request->image->store('images', 'public');
return 'image uploaded succesfully';
but in my webiste it tells me page the url you requested is not found on the site serve
You've defined your route using POST meaning it will only respond to POST requests.
If you try to access /upload from a web browser, that uses a GET request which you've not defined a route for. So you want to define such a route:
Route::get('/upload', function () {
return view('upload.blade.php');
});
You'll want to replace upload.blade.php with the name of your view that has your upload form in it.
Name Your Route 1st , hit this command php artisan route:clear then try..
Change the form action action="{{ route('upload') }}"
<div class="card-body">
<form action="{{ route('upload') }}" method="post" enctype="multipart/form-data">
#csrf
<input type="file" name="image">
<input type="submit" value="upload">
</form>
</div>
This is blade template:
<form action="/fileupload" action="post" enctype="multipart/form-data">
#csrf
<input type="file" name="file"><br>
<button type="submit">submit</button>
</form>
This is the route:
Route::get('/fileupload', [FileUpload::class,"upload"]);
I try to create a function to uptade a password.
in my controller:
public function updatePassword($id)
{
$user = User::find($id);
dd($user);
}
my button to open my page for update password
<div class="float-end">
<form method="POST" action="myRoute.updatePassword">
#csrf
#method('PUT')
<button type="submit" class="btn btn-primary">
Modifier le mot de passe
</button>
</form>
</div>
and my route :
Route::put('admin/update_password/{id}','MyController#updatePassword')->name('myRoute.updatePassword');
And when i click on the button, i have this error :
The GET method is not supported for this route. Supported methods: PUT.
Thanks for you'r help !
Your form action:
<form method="POST" action="myRoute.updatePassword">
Should be:
<form method="POST" action="{{ route('myRoute.updatePassword', $userId) }}">
You should pass the Id as a parameter since this is expected in your route and in your updatePassword method.
You can check more info about Laravel Routing in the docs: https://laravel.com/docs/8.x/routing
<form method="POST" action="myRoute.updatePassword">
Should be
<form method="POST" action="{{ route('myRoute.updatePassword') }}">
More information: https://laravel.com/docs/8.x/urls#urls-for-named-routes
i have form in my delete modal, and i want to pass hidden input to url method delete/destroy
this is my delete modal :
<form action="{{url('/pages').'/' ... }}" method="POST" enctype="multipart/form-data">
{{ method_field('DELETE') }}
{{csrf_field()}}
<div class="form-group">
<label for="">Are you sure to delete this Page ?</label>
<input type="text" class="form-control" id="page_id" style="display: none;">
</div>
<button type="submit" class="btn btn-danger" id="save">Delete</button>
<button type="button" class="btn btn-link" data-dismiss="modal">{{__('Close')}}</button>
</form>
how to append input value(page_id) to form action ?
so i can use that page_id in form action :
<form action="{{url('/pages').'/'page_id}}" method="POST" enctype="multipart/form-data">
can some one help me ? thanks before :)
You have to add a new attribute on each delete button contains the id.
please check the following code
$('.delete-button').click(function(){
var pageId = $(this).data('page-id');
$('.page_id').html(pageId);
$('#modal-form').attr('action', deleteUrl+pageId);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
page 1
page 2
page 3
<form action="#" id="modal-form" method="POST" enctype="multipart/form-data">
<label for="">Are you sure to delete this Page <span class="page_id"></span>?</label>
</form>
<script>
/* window.deleteUrl = "{{url('/pages').'/'}}"; // save globally */
window.deleteUrl = "http://test.com/"; // temporary
</script>
If you are using boostrap modal then you have to pass data from that button something like this,
<button id="submit-btn" data-action="{{route('test')}}">Submit</button>
In JS,
replace modal's action with data-action.
Something like this,
$('#form-id').val($('#submit-btn').data('action))
And if you are not using modal then just store that id in variable and pass in route directly.
Hope it helps :)
I can figure out why i am not accessing me destroy method.
Blade:
#foreach($advertisements as $advertisement)
<form class="form-horizontal" method="delete" action="advertisements/{{ $advertisement->id }}" accept-charset="UTF-8">
<div class="form-group">
<h2> {{ $advertisement->title }}</h2>
{{$advertisement->city}} {{ $advertisement->type }}
<input class="btn-danger" type="submit" value="Delete add">
</div>
</form>
Controller:
public function destroy($advertisements)
{
Advertisement::find($advertisements)->delete();
return redirect('advertisements');
}
route:list
| DELETE | advertisements/{advertisements} | advertisements.destroy | App\Http\Controllers\AdvertisementsController#destroy
From official laravel docs,
HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
You should update your form tag and add a hidden input field _method:
<form class="form-horizontal" method="POST" action="advertisements/{{ $advertisement->id }}" accept-charset="UTF-8">
<input type="hidden" name="_method" value="DELETE">