Web Hosting Talk







View Full Version : Javascript Form


Hybrid|Kron!c
03-10-2005, 09:19 AM
hey everyone, im wondering if anyone knows how i can set up a javascript form so that when people select an option it shows the total price of what they have on the screen. i heard this was javascript, might not be but if u know what i mean let me know :D

the_pm
03-10-2005, 10:07 AM
This is fairly easily done using JavaScript, though it takes a little work to create the script. It's generally more reliable to do these calculation in a server-side scripting language, such as PHP or ASP, but JavaScript is a viable option if it's not a mission-critical type of thing. Can you give more specifics? Maybe I can throw together a little sample script for you.

Hybrid|Kron!c
03-10-2005, 01:28 PM
this is an example of what i mean.. HERE (http://www.e-frag.co.uk/?page=services)

when u press on a certain game type it give u the price.. something like that or simpler but same sort of thing would be great

the_pm
03-10-2005, 01:43 PM
Here's an idea of how this function works, in its simplest form:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">
function calc() {
document.calculate.total.value = parseFloat(document.calculate.server.value) + parseFloat(document.calculate.players.value)
}
</script>
</head>

<body>

<form name="calculate" action="" method="post">

<p><select name="server" onchange="calc()">
<option value="0">Choose a server
<option value="5">Tiny Server
<option value="10">Medium Server
<option value="15">Gigantic Server
</select></p>

<p><select name="players" onchange="calc()">
<option value="0">Number of players
<option value="5">5
<option value="10">10
<option value="15">15
</select></p>

<p>$<input type="text" name="total" value="0" size="2"></p>

</form>

</body>
</html>
Copy/paste that into a blank document and save it as .html. You'll see exactly how it works - it's very easy to expand. Don't mind the older markup. I didn't feel like moving the JS statement into an external document to comply with XHTML standards, but you can do that easily enough on your own, I'm sure.

HTH! :)

Hybrid|Kron!c
03-10-2005, 02:30 PM
thank you so much.. i can reall work of that.. thx alot :D