pengi Posted August 21, 2013 Share Posted August 21, 2013 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 <(") Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/ Share on other sites More sharing options...
Psycho Posted August 21, 2013 Share Posted August 21, 2013 You already have the four submit buttons which are defined as "Input" buttons which have a corresponding value. You don't need the hidden inputs since you can use the values passed from the submit buttons Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/#findComment-1446080 Share on other sites More sharing options...
pengi Posted August 21, 2013 Author Share Posted August 21, 2013 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 <(") Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/#findComment-1446084 Share on other sites More sharing options...
Solution kicken Posted August 21, 2013 Solution Share Posted August 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/#findComment-1446087 Share on other sites More sharing options...
Psycho Posted August 21, 2013 Share Posted August 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/#findComment-1446110 Share on other sites More sharing options...
pengi Posted August 21, 2013 Author Share Posted August 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/#findComment-1446120 Share on other sites More sharing options...
Psycho Posted August 21, 2013 Share Posted August 21, 2013 But I cannot create the scenario when the value of none of the submit buttons is sent. It would be based upon the browser. So, the behavior could be different between brands of browsers or even different versions of the same browser. Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/#findComment-1446121 Share on other sites More sharing options...
kicken Posted August 21, 2013 Share Posted August 21, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/281421-is-a-good-practice/#findComment-1446125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.