XML technologies / CSS / CSS Cookbook / Forms / Buttons that can be used only once
(Excerpt from “CSS Kochbuch” by Christopher Schmitt)
Problem
You want to prevent users from clicking a submit button more than once.
Solution
First, create a class that hides the button:
.buttonSubmitHide {
display: none;
}Then use the following JavaScript code, already familiar from the preceding recipe, to switch styles through class selectors:
<script language="JavaScript" type="text/javascript">function classChange(styleChange,item) { item.className = styleChange; }</script>The onsubmit event calls the function defined above, hiding the button after the first form submission:
<h2>Ihre Bestellung</h2>
<form action="login.php" method="post" onsubmit="classChange('buttonSubmitHide',submit); return true; ">
<div align="center">
<p>Wollen Sie wirklich 12 Dosen Blubberwasser im Web kaufen?</p>
<label for="uname">Endsumme:</label>
<input type="text" name="uname" id="uname" value="€ 7.95" />
<br />(19% Mehrwertsteuer enthalten, Porto und Verpackung extra)<br />
<input type="submit" name="submit" value="Jetzt bestellen" class="buttonSubmit" />
</div>
</form>Discussion
In this solution, the JavaScript function switches the style rules for the submit button. We use the onsubmit event to call the function so that the form is also submitted. If we used an onclick event instead, some browsers would only change the CSS class, hiding the button and preventing the form from being submitted.
See also
The JavaScript & DHTML Cookbook by Danny Goodman (O’Reilly), for more recipes combining JavaScript and CSS.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln