1

Could you please tell me the difference between submitting a submit button value and submitting a hidden input value?

I ask the question especially in regard of the browser compatibility (IE 9+).

So, my problem is actually the following: I know that a posted hidden input value is always correctly read by the server. But I'm not sure if this is the case too, if the value (which I want to post) is part of the "value" attribute of a <button> tag.

Thank you very much for your time!

P.S: I prepared an example for clarity. It contains a form for each option in my question. When one of the forms is submitted, the corresponding values are read in PHP. Relevant is the user id value.

<?php
if (isset($_POST['submitUserId']) && !empty($_POST['submitUserId'])) { // Submitted values.
    $userId = $_POST['submitUserId'];
    $userName = $_POST['userName'];

    echo 'Posted user id: ' . $userId;
    echo '<br/>';
    echo 'Posted user name: ' . $userName;

    // Save the new values in db...
} else { // Initial values fetched from db.
    $userId = 123;
    $userName = 'Valentine';

    echo 'Initial user id: ' . $userId;
    echo '<br/>';
    echo 'Initial user name: ' . $userName;
}
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Test - Submit button value vs. hidden input value</title>
    </head>
    <body>

        <h4>Option 1: Submit <i>user id</i> as submit button value</h4>

        <form action="" method="post" id="formButtonValue" name="formButtonValue">
            <input type="text" id="userName" name="userName" value="<?php echo $userName; ?>" />

            <button type="submit" id="submitUserId" name="submitUserId" value="<?php echo $userId; ?>">
                Submit user details
            </button>
        </form>

        <h4>VS.</h4>

        <h4>Option 2: Submit <i>user id</i> as hidden input value</h4>

        <form action="" method="post" id="formHiddenInputValue" name="formHiddenInputValue">
            <input type="text" id="userName" name="userName" value="<?php echo $userName; ?>" />

            <input type="hidden" id="submitUserId" name="submitUserId" value="<?php echo $userId; ?>" />

            <button type="submit" id="submitButton" name="submitButton">
                Submit user details
            </button>
        </form>

    </body>
</html>
9
  • the submit .. trasmit data to the server .. all the data in inout field hidden or not ..the only diferrence is that in one case you see the filed and in the second you don't see . Commented Aug 20, 2017 at 16:20
  • 1
    Use a hidden input if you want to send data in addition to the submit button and other visible inputs. Commented Aug 20, 2017 at 16:26
  • 1
    There can nearly be infinitely many hidden inputs with values, but just one submit value when you send the form. Both will be read by the recipient, and it doesn't care or know if it was a submit-button or a hidden value. Commented Aug 20, 2017 at 16:29
  • 1
    all the content of the value atrruibutes (inside the form) are trasmitted also for button typical assign different value to different submit button for obatin the button pressed . and manage the server for different action . Commented Aug 20, 2017 at 16:34
  • 1
    A browser will send form data from the form input fields, it won't differentiate what sort of input field that data is from. A text input will be the same as a number input or a textarea input a hidden input or a button input. Commented Aug 20, 2017 at 16:38

2 Answers 2

1

In the submitting data process, there's no difference at all.

The server side will never know, or care, about how the input being displayed or processed on the client side.

In fact, in your code, the value's, as the one in hidden input, and the one in the button, are both hidden to the user.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer, @Lee. I reedited my question with two phrases regarding browser compatibility and my actual problem. Could you please tell me, if there is still no difference when I use a IE9+, for example? Do you know some side effects, maybe?
I cannot put hands on ie currently. But I believe there's won't be any side effect, since this is very basic html implementation.
Yes, that's what I think too. I know that this is a base html implementation, but I have in mind that some years ago there were some side effects when using button values and I don't know if they were resolved. Especially in IE. I really appreciate your answer!
0

There is no difference

Submit user id as submit button value(var_dump($_POST))

array(2) { ["userName"]=> string(9) "Valentine" ["submitUserId"]=> string(3) "123" } 

Submit user id as hidden input value(var_dump($_POST))

array(3) { ["userName"]=> string(9) "Valentine" ["submitUserId"]=> string(3) "123" ["submitButton"]=> string(0) "" } 

1 Comment

Thanks, @syntaxe. I reedited my question with two phrases regarding browser compatibility and my actual problem. Could you please tell me, if there is still no difference when I use a IE9+, for example? Do you know some side effects, maybe?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.