Jump to content

Is <input type="hidden"> a good practice?


pengi
 Share

Go to solution Solved by kicken,

Recommended Posts

Hi everybody,

 

I am writing an inventory system using PHP+MySQL. The system provides simple functions like Insert, Display, Update and Delete for the user to choose in Page 1. However, for Display, Update and Delete, users then have to enter the product ID in page 2, but not for the Insert function, which should directly jump to Page 3 for inserting the item specifics.

 

As the different functions can go to different pages, in Page 1, I don't think I shall put all these function choices as radio buttons in the same form. One solution is to implement these 4 choices as 4 submit buttons, i.e. 4 forms are created. However, to differentiate among Display, Update and Delete, I need the HTML of Page 1 to carry some values to Page 2 depending on which submit button is being chosen, but apart from the choice of the 4 functions, users do not need to input anything else in Page 1. Hence I need a hidden input type, like this:

 

<form name='1' action='Page3.php' method='post'>  <!-- Insert will skip Page 2 -->

<input type='Submit' value='Insert'>

</form>

<form name='2' action='Page2.php' method='post'>

<input type='hidden' name='function' value='Display'>

<input type='Submit' value='Display'>

</form>

<form name='3' action='Page2.php' method='post'>

<input type='hidden' name='function' value='Update'>

<input type='Submit' value='Update'>

</form>

<form name='4' action='Page2.php' method='post'>

<input type='hidden' name='function' value='Delete'>

<input type='Submit' value='Delete'>

</form>

 

So in Page 2, I can know which button is clicked from $_POST['function']. I believe this works, but is it a good practice? If not, is there any better solution?

 

Thanks!

 

--

pengi <(")

 

 

 

 

 

Link to comment
Share on other sites

Thank you very much.

 

Just one more question: In such case (using multiple submit buttons without any hidden item), is the value passed from the submit button always the same as the text shown inside the button (since both rely on the content of value)?

 

--

pengi <(")

Link to comment
Share on other sites

  • Solution

Yes, the value passed would be what you put into the value="" attribute, which is also what is displayed.

 

In addition, you do not need several forms. Since your Display/Update/Delete go to the same page, they can all be in one form.

<form name='1' action='Page3.php' method='post'>
<input type='Submit' name="function" value='Insert'>
</form>
<form name='1' action='Page2.php' method='post'>
<input type='Submit' name="function" value='Display'>
<input type='Submit' name="function" value='Update'>
<input type='Submit' name="function" value='Delete'>
</form>
On page2.php, $_POST['function'] will be set to either Display, Update, or Delete depending on the button clicked.
Link to comment
Share on other sites

You have to be careful about putting multiple submit buttons with different values into a form. A user can submit a form by pressing the enter key. AFAIK, this only happens where the focus is within an input field: e.g. text, textarea, etc. Currently, I'm seeing that the value from the first submit button is sent. But, I seem to recall that there were instances where the value of a submit button is not sent at all when enter is used to submit a form. So, I guess I'm going to go back on my previous statement and suggest that you do add hidden fields to the forms.

Link to comment
Share on other sites

Thank you both for the answers. :)

 

I have done a little test on the situation with multiple submit buttons in a form, and also a text field. Yes, pressing enter once the text is entered to the text field will send the value of the first submit button. But I cannot create the scenario when the value of none of the submit buttons is sent.

 

Anyway, since my situation does not involve other input field at all, I suppose it is safe to implement with or without hidden fields.

Link to comment
Share on other sites

Back in the day (IE 5/NS4/etc) what happened when you submitted a form with the enter key varied from browser to browser. I forget what each browser did exactly.

 

At this point, last I checked anyway, it seems that the browsers have all pretty much standardized on sending the first button name/value pair. The HTML5 spec seems to dictate this as the standard behavior:

 

A form element's default button is the first submit button in tree order whose form owner is that form element.

 

If the user agent supports letting the user submit a form implicitly (for example, on some platforms hitting the "enter" key while a text field is focused implicitly submits the form), then doing so for a form whose default button has a defined activation behavior must cause the user agent to run synthetic click activation steps on that default button.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.