Forum Moderators: open
<button type="submit">Okay</button> tag instead of the <input type="submit" value="Okay"> But this had some unexpected consequences.
The old html looked like this...
<form method="get" action="thispage.php">
<div>
<input type="submit" name="okay" value="Okay">
<input type="submit" name="cancel" value="Cancel">
</div>
</form>
But when I switched to the new code, which looks like this..
<form method="get" action="thispage.php">
<div>
<button type="submit" name="okay">Okay</button>
<button type="submit" name="cancel">Cancel</button>
</div>
</form>
So how do I figure out which button was pressed when I'm using the <button> tag? Is there something else I can add to the html?
I'd like to stick with the <button> tag if possible, because its a lot more flexible and easier to style with css. And I don't want to have to add a client-side solution like a javascript.
Thoughts?
<form method="get" action="thispage.php">
<div>
<input type="hidden" name="whichone" value="">
<button type="submit" onclick="this.form.whichone.value=okay;" name="okay">Okay</button>
<button type="submit" onclick="this.form.whichone.value=cancel;" name="cancel">Cancel</button>
</div>
</form>
Unfortunately, I need a page that doesn't use Javascript so the onclick handlers solution is a non-starter. :(
> Have you tried type="reset" on the cancel button?
Yup, but reset just sets the form data back to its original values - it doesn't cause a submit or affect the url.
Surely there must be some way to do this or is it just a gaping hole in the standard?
I might have to go back to <input type="submit"> :(
You could try setting the id="..." attribute of each of the buttons to a unique value. (Just a guess; I haven't had a chance to play with it myself)
Shawn
Other than that, I'd think you'd have to use either the <input> tag or javascript. Or, if you're using ASP.NET on the server side, you might be able to work something out with post-back and server controls.
Luck,
g.
I know you don't want to use Javascript, but in case anyone else is trying to do a similar thing...
This should work
<form method="get">
<div>
<input type="button" value="Okay" onClick=location.href="thispage.php?okay=Okay">
<input type="button" value="Cancel" onClick=location.href="thispage.php?cancel=Cancel"></div>
</form>
I don't think that will work well. What will happen is you will get 2 calls to the .php page. The first with "...?okay=Okay", and the second (before the browser has finished loading the first) with "...?all the form variables". It will be as if the user cancelled his action with the first post, went back, and submitted the form again, but this time without the stuff which identifies the button. If you stop the processing of the second call with "return false;", the first call contains none of the form data, other than which buitton was pressed.
Opera7 and Netscape7 only add a value in the query string for the button you press. IE always adds both.
A clear breach of the HTML4.01 forms spec [w3.org] which says..
Every successful control has its control name paired with its current value as part of the submitted form data set.. yada..yada.. If a form contains more than one submit button, only the activated submit button is successful.
In fact it gets worse than that. The button tag [w3.org] is supposed to offer you rich rendering because it can contain html, but if you do something like this..
<form method="get" action="thispage.html">
<p>
<button name="okay" value="1"><img src="prettybutton.gif"><br><b>Okay</b></button>
</p>
</form>
It will produce a query string like this_page.html?okay=1 in Opera and NN but IE will produce this instead..
thispage.html?okay=%3CIMG+src%3D%22prettybutton.gif%22%3E%3CBR%3E%3CB%3EOkay%3C%2FB%3E
..encoding the entire html content of the button tag instead of its value.
Sad sad sad :( :(
Thanks for everyones help, but I think I'll have to go back to the <input type="submit"> style. At least that works as documented.
Now if I could just figure out how to write a CSS selector so I could style those <input> buttons without having to give them all classes ( input[type=submit] should work but doesn't in IE! )
(PS Shawn: setting id had no effect either. Garann: two forms is the only way I can see to make it work too but given the flawed implementation of button I'm giving up)