Hi guys, I was wondering whether it is alwasy true that a button needs to be inside a form tag? Is that necessary all the time?

The W3C say that The FORM element acts as a container for controls

so kind of yes really, but I 'd like to see if anybody has a different opinion. The thing is, I am working on a simple page at th moment which has 3 buttons, each of them doing a different thing: the first button validates something online; the second validates something offlice; the third cancels the action. So ideally each of them should be inside a separate form tag don't you think? To save me the hassle of having 3 form tags I was thinking not to have it at all, so just buttons inside a container, no form tag involved, but I am not sure that is a good idea

Recommended Answers

All 6 Replies

If you remove the form tag you'll just have to trigger your actions with Javascript. Depending on what you need to do this may be just fine.

pritaeas, I think I might have made a mistake in the way I have formulated the question. It didn't occur to me (yes I must have forgotten!) that buttons tag can have a type="button" which in a way it solves the problem. So really my question should have been: can I have more than 1 submit button inside a form tag? From what I can see, yes I can as long as they have a different name attribute. SOrry for the confusion.

Yes, you can. They will all share the same form action of course. If they need to be different, you'll have to use some Javascript.

ok I see, how about more than one submit button with a different name attribute? would that be enough to somehow "differentiate" them?

Server side, yes. Only the submit button that was clicked will be set in the POST data.

Hi,your question might confusable can you explain it clearly so that it will be easy for me to give you solution....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.