No need to check if Laravel can see it. Check in developer tools! The Network tab is amazing for checking headers/form data.
Submit Button Value not available in Request
I am working on a project to create ads for rental properties. In my form to edit the ad I have some buttons with varying options. I simply pass a status to the button so that my controller can set the status correctly in the Listing.
The problem I am having is that the <button type="submit" name="status" value="[valuehere]"> is not being seen (or at least passed to Laravel Request object).
I do have the same buttons at the top and bottom of my page since the page is so long. However I tried removing one set to see if that solves the problem, and it does not.
Here is a short version of my form
<form method="post" action="{{ route('my-route-that-works') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
{{-- Publish and Save Draft at top of page --}}
<button type="submit" name="status" value="Available" class="btn btn-success form-control"><i class="fa fa-fw fa-thumbs-o-up"></i> Publish</button>
<button type="submit" name="status" value="Draft" class="btn btn-danger form-control"><i class="fa fa-fw fa-sign-out"></i> Save Draft</button>
{{--
Other 20 Form Variables here
--}}
{{-- Publish and /Save Draft at bottom of page --}}
<button type="submit" name="status" value="Available" class="btn btn-success form-control"><i class="fa fa-fw fa-thumbs-o-up"></i> Publish</button>
<button type="submit" name="status" value="Draft" class="btn btn-danger form-control"><i class="fa fa-fw fa-sign-out"></i> Save Draft</button>
</form>
In my controller action, when I do try to access the value of status it's not showing up at all. It simply doesn't exist.
public function postFormHandler(Request $request)
{
dd($request->all());
}
The code above will show all form inputs...except the status input. Furthermore
dd($request->input('status')); // shows null
dd(\Input::input('status')); // shows null
dd(\Input::all()); // shows all forms values except status
dd(\Input::post('status')); // shows null
dd($_POST['status']); // throws an error of 'index status not found'
So I am thinking it's more of a PHP error (unless laravel empties the $_POST array which is possible).
Any ideas?
EDIT
I am using Laravel 5.1, Nginx, PHP 5.6, no homestead (homebrew mac).
The reason the status wasn't showing up in the POST is because it wasn't being sent with the POST. Thanks to @bashy and his suggestion to look at the developer tools in chrome I was able to see that.
That led me to my javascript which was doing an e.preventDefault() and then later triggering the submit. So javascript was submitting the form, not the button.
I updated my buttons as such
<button type="submit" name="status" value="Available" class="btn btn-success form-control" onClick="return validate()"><i class="fa fa-fw fa-thumbs-o-up"></i> Publish</button>
<button type="submit" name="status" value="Draft" class="btn btn-danger form-control" onClick="return validate()"><i class="fa fa-fw fa-sign-out"></i> Save Draft</button>
I then extracted my validation from a jquery onSubmit handler to the validate function. By simply having the validate function return false, it would prevent the form from being submitted allowing the errors to show. By having the validate function return true it let the form submit as normal and the value of the button used is passed.
SPECIAL NOTE
Whenever I submit a form I like to add a disabled attribute to the form buttons so that someone can't click it if they have a slow connection or if the response is taking too long. I generally replace the button text with an ajax style spinning icon and then replace the button text if needed and remove the disabled attribute if needed in case there are form errors.
I noticed return true did not work if I kept the disabled attribute on the button. Just an FYI in case anybody else tries that.
Please or to participate in this conversation.