Laravel giving a html form a unique url - php

In my blade I have field called $jobs. I am trying give every single form a unique url based on the $job->id. So the url is jobs/$job->id. However when I click on the a tag for to submit the form all the urls show jobs/the last $job->id. In my case all the urls show job/386. What should I so every url has a unique url? Here is my code.
#foreach($jobs as $job)
<form method="post" action="{{url('jobs/'. $job->id)}}" id="start-jobs">
{{csrf_field()}}
<a onclick="document.getElementById('start-jobs').submit()">( start )</a>
</form>
#endforeach

This is actually a JavaScript problem, not a Laravel problem.
All your forms have the same id (which isn't actually valid HTML), and which is why getElementById('start-jobs') is getting you the last one. If you need to have an identifier for all of the forms, use class instead of id.
It seems like you should be able to use just a regular submit button instead of the submit link you're using.
#foreach($jobs as $job)
<form method="post" action="{{url('jobs/'. $job->id)}}" class="start-jobs">
{{csrf_field()}}
<input type="submit" value="( start )">
</form>
#endforeach

Related

GET parameter won't pass with html button formaction

I am trying to pass a GET parameter through a button but I can't figure out what I am doing wrong. The parameter is set, as it shows up fine in the header, but it isn't being added to the edit.php url. The button is directing me to edit.php, just without the GET parameter added. I am pretty new to this stuff and this is my first time using links that aren't through anchor tags, so I am clearly missing something here. Any advice is greatly appreciated.
<h1 class="headerWithButton">Claim #<?echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
</h1>
When you submit a form using the GET method, any existing query string in the action will be replaced by a new one generated by the name and value of the successful controls associated with that form.
In your case, the only successful control is the submit button, which doesn't have a name or a value.
You could get the effect you desire by moving the data to those attributes:
<h1 class="headerWithButton">Claim #<?php echo htmlspecialchars($_GET['claim_id']); ?>
<form>
<button formaction="index.php" class="backButton">Back</button>
<button formaction="edit.php" name="claim_id" value="<?php echo htmlspecialchars($_GET['claim_id']); ?>" class="editButton">Edit</button>
</form>
</h1>
Important security note: inserting data from the URL directly into a page makes you highly vulnerable to XSS attacks. You need to take precautions against that. The most basic of those is using htmlspecialchars.
Note, however, that it isn't really appropriate to use a form here. Your form buttons are not submitting any data the user has entered, nor performing any kind of action. The affordances offered by buttons are misleading here.
You can, and should, use regular links instead.
<form method="get" action="edit.php">
<?echo('<button type="submit" formaction="edit.php?claim_id='.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
instead of using the form you can just use a straightforward link
k with the anchor tag
edit
or you can specify the methos of get on the form with a hidden for input to place the link get parameter
If you have to use formaction, you must specify name and value of element:
<h1 class="headerWithButton">Claim #<? echo($_GET['claim_id'])?>
<form>
<button type="submit" formaction="index.php" class="backButton">Back</button>
<?php echo('<button type="submit" formaction="/edit.php" name="claim_id" value="'.$_GET['claim_id'].'" class="editButton">Edit</button>');?>
</form>
here it is better to place buttons in different blocks. But personally, in this case, I use a hyperlink
<form method="get" action=""></form>
<?echo('Edit
I find the easiest way to pass values with a button is to just wrap a form around the button.
$id = $_GET['claim_id'];
echo <<<EOT
<form action="index.php" method="get">
<button>Back</button>
</form>
<form action="edit.php" method="get">
<button name="claim_id" value="$id">Edit</button>
</form>
EOT;

Laravel - How to pass variables from view to controller with form

I'm building a request system with Laravel 7. In the index page, there is a form for user to fill in and a submit button. If the button is clicked, the data in the form will be sent to the supervisor via email. The email which the supervisor receives, all user input data will be listed along with a "Approve" button and a "Reject" button. If the "Approve" button is clicked, the user input data will be stored in the database. All user input data is stored in $data and can be accessed in email template view. So, in the email template, how do I pass the user input data and Auth::user()->id to the controller so that the storing process will start?
I read this post and it looks useful to me, but it is using <a> to do the redirection to trigger the controller. Is it possible to pass the blade variables from the <form> in the email template view to the controller, so that I can use <button>?
Update:
index.blade.php:
<form action="/sendSupervisor" method="post">
<input type="text">
<input type="text">
<button type="submit">Send</button>
</form>
web.php:
Route::post('/sendSupervisor', 'CatViewController#sendSupervisor')->middleware('auth')->middleware('verified');
CatViewController.php:
// one of the methods
public function sendSupervisor()
{
$data = request();
Mail::to('supervisor#abc.com')->send(new ApprovalMail($data));
return redirect('/');
}
approval.blade.php:
<!-- Email Template -->
User Input: {{ $data->name }}
<form action="/approve">
<button>Approve</button>
</form>
<form action="/reject">
<button>Reject</button>
</form>
An email is not a browser, and in almost every case a <form> tag will have no effect. You cannot retrieve user input from an email, however in your case if you have two buttons in the email, and that email is being sent to a user that exists in your system, AND the input you are receiving is 'accept' or 'reject' based on which button they click, this is doable.
You can have two routes that are publicly accessible and build two "buttons" with hrefs that will carry out those actions. Like:
Route::get('email/accept/{user_id}', 'EmailController#accept');
Route::get('email/reject/{user_id}', 'EmailController#reject');
Then link to those routes in your email temple like:
Accept
Reject
Then in that controller, you can resolve the user based on id in those methods, and you can carry out any logic you would like.
A note of caution: since these links need to be publicly accessible, using id's is not ideal. You should generate some sort of token to resolve the user.
A better approach:
It's better to save the user request once he post it with a column flag called "status" that holds either ("Pending", "Approved", "Rejected"), and then send an email to the supervisor
index.blade
<form action="/sendRequest" method="post">
<input type="text">
<input type="text">
<button type="submit">Send</button>
</form>
web.php
Route::get('/sendRequest', 'MyController#sendRequest)
MyController.php
public function sendRequest(Request $request){
// UserRequest table should has column 'status' with default value 'Pending'
$userRequest = UserRequest::create($request->all());
Mail::to('supervisor#abc.com')->send(new ApprovalMail($userRequest));
return redirect('/');
}
The email template should only notify the supervisor to take an action with the request, with a button 'Take action' that opens a page in the browser for him to take the action.
email-template.blade
<!-- User data displayed for example in a table -->
<a href={{ route('takeAction', $userRequest->id) }}>
<button> Take Action <button>
</a>
web.php
Route::get('/take-action/{requestId}', 'SupervisorController#takeAction')
->name('taleAction');
SupervisorController.php
public function takeAction($requestId){
$userRequest = UserRequest::find($requestId);
return view('take-action', [$userRequest]);
}
take-action.blade
<-- User data displayed for example in a table -->
<form method='POST' action={{ route('takeActionPost', $userRequest->id) }}>
#csrf
<input type='hidden' name='action' value='Approved'>
<button type='submit'>Approve</button>
</form>
<form method='POST' action={{ route('takeActionPost', $userRequest->id) }}>
#csrf
<input type='hidden' name='action' value='Rejected'>
<button type='submit'>Rejected</button>
</form>
web.php
Route::post('/take-action/{requestId}', 'SupervisorController#takeActionPost')
->name('takaActionPost');
SupervisorController.php
public function takeActionPost(Request $request, $requestId){
$userRequest = UserRequest::find($requestId);
$userRequest->update(['status', $request->action]);
}
Of course the names I'm using here are very bad, I just want to explain my idea
Add hidden input fields in the form whose values are the user data. But I think there is a better design for your system that I'll explain in a separate answer.

After rewriting some of my routes using put, patch and delete my functionality doesn't work anymore and the page just refreshes. Not getting errors

After finding out I've been writing my routes slightly wrong I decided to fix them. The routes originally looked like this:
Route::get('/deleteImage/{id}', 'ArtworkController#deleteImage')->name('deleteImage');
Route::get('/deleteCategory/{id}', 'CategoryController#deleteCategory')->name('deleteCategory');
Route::patch('/profile/{id}/update', 'UsersController#updateProfile')->name('updateProfile');
And I changed them to:
Route::delete('/image/{id}', 'ArtworkController#deleteImage')->name('deleteImage');
Route::delete('/category/{id}', 'CategoryController#deleteCategory')->name('deleteCategory');
Route::patch('/profile/{id}', 'UsersController#updateProfile')->name('updateProfile');
Sadly, as soon as I changed Route::get('/deleteImage/{id}' to Route::delete('/image/{id}' something went wrong.
This is the <a> tag that leads to the deleteImage route:
<a class='placeholderDelete' href='{{ route('deleteImage', ['image_id' => $image->id]) }}'>Delete Image</a>
Before making the changes the image would get deleted after clicking that anchor element, however, after the changes the page just refreshes and nothing happens. The image doesn't get deleted from my database and storage.
I'm not sure how to troubleshoot as I'm not getting any errors or anything like that.
DELETE routes need to be handled by a form that uses POST. Normal links (anchor tags) visit a page using a GET request. You will need to use a form. Here is an example.
<form id="delete-form" class="form" role="form" method="POST" action="{{route('deleteImage', $image->id)}}">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<button type="submit">Delete</button>
</form>
If you want to use a regular link to do the deletion, you could hide the form and use javascript to submit it.
Delete Image

Laravel 5 : Why is my request->input returning null

I'm trying to return the value of a checkbox within my laravel controller, but every time I request a input from a checkbox element in a form, it returns null.
My controller, retrieving the input of a element called Filter-Method.
Here I'm trying to request a input method called filter-method which is a checkbox.
My Route, since this function will execute on a button:
My Blade, where I'm trying to retrieve the result of my filter-method checkbox:
On line 38 I have a checkbox called filter-method, and when you click on the button on line 115 it should send a request to the controller where it would return a result but instead it returns null
Any ideas of why I'm returning null?
You are not passing any parameter named filter-method. If you are posting values you should use post method.
Like following
Route::post('GetFilterByColumns','MentorController#FilterByValuesColoumns')
If you want to list data according to filter-method then try the following.
Route::get('GetFilterByColumns/{filter-method}','MentorController#FilterByValuesColoumns')
And in your mentorlist.blade.php page
change the href value according to route.
You have to add form to your blade around checkbox either with get or post method as per your requirement change route according to your form method
consider demo blade file
<form method="get" action="{{ url('GetFilterByColumns') }}" class="form-horizontal form-label-left" id="">
<div class="checkbox">
<label><input type="checkbox" name="filter-method" value="filter-method>Method</label>
</div>
<input type="submit" value="submit"/>
</form>
Now you will get a checkbox value in contoller
This is very basic thing just use form instead of <a> tag. Every time you need to send input value to server you need to use <form> element. In image you have uploaded there is no <form> and you are using a <a> tag.
You need to do it like this
<form method="get" action="/getFilterBycolumns">
<input type="checkbox" name="filter-method">
// other input fields
// and then a submit button instead of <a>
<button class="your-class" type="submit"> Send</button>
just use POST instead of GET if you are sending form values

Next page can't be found if input on previous page was filled

I'm making a form on Wordpress website. My problem is easiest shown in practice: try submitting empty form (first one, black) on http://newzapchasti.evella.ru/ and it will redirect to the next page. Then try filling out the last input field of this form and submit it again. The same page (http://newzapchasti.evella.ru/quest.html) isn't found this time.
My code is very simple:
<form action="/quest.html" method="post">
<input name="year" type="text">
<button type="submit">send</button>
</form>
Why does it happen?
On the second page the form action is
<form action="http://newzapchasti.evella.ru/" method="post">
While on the first one is
<form action="/quest.html" method="post">
That may be the reason :)
Try changing input field name attribute to other values, some names could make existing page display as 404.

Categories