Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BENderIsGr8te's avatar

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).

0 likes
5 replies
bashy's avatar

No need to check if Laravel can see it. Check in developer tools! The Network tab is amazing for checking headers/form data.

1 like
BENderIsGr8te's avatar

I really need to explore the developer tools. I use them a lot, but not to their full potential. I actually never realized I could see form data there.

At any rate, in the dev tools in Chrome, Safari and Firefox the status is not being posted. I realized I have some javascript doing some validation, but the javascript doesn't submit the form, the javascript only sends an e.preventDefault() if there is a missing required field and then displays the message.

I commented out the validation and the post worked with status being available. So now I know where to continue my debugging.

BENderIsGr8te's avatar

I know what the problem is. I just need to figure out a better solution.

My form has a JQuery event handler

$('#listingForm').submit(function(e){
    e.preventDefault(); // otherwise the form will submit even with errors
    
    // blah blah blah error checking

    if( errors) {
        $('#errorDiv').html(errorMessage).slideDown();
        return false;
    }

    $(this).unbind('submit').submit()
});

So the form is stoping the submission, doing it's error checking and then displaying a message if the error is found. However if there is no error it unbinds that form and then tells it to re-submit. The problem is that now the button being pushed isn't what submitted it, ajax is.

I'll either have to add an onClick handler to the buttons that return true or false (I think that should prevent form submissions if returning false) or I'll have figure out a better way to validate and show errors.

bashy's avatar

Yeah the network tab is great to quickly see the POST data, saves you checking in the code and trying to dump all the request vars.

Yeah if it's not being sent in the request, PHP has no chance of getting it :P

BENderIsGr8te's avatar
BENderIsGr8te
OP
Best Answer
Level 6

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.

1 like

Please or to participate in this conversation.