View Single Post
Old 09-02-2010, 07:43 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Something like this:-

Code:
<select id = "sel1" onchange = "assignPrices()">>
<option value="A">Product 1</option>
<option value="B">Product 2</option>
<option value="C">Product 3</option>
</select>

<script type = "text/javascript">
function assignPrices() {
var price = 0;
var now = new Date();
var mth = now.getMonth();   // note that in Javascript months are 0-11
var val = document.getElementById("sel1").value;
if (mth == 8) {  // September
switch (val) {
case "A": price = 100; break;
case "B": price = 150; break;
case "C": price = 200; break;
}
}
if (mth == 9) {  // October
switch (val) {
case "A": price = 50; break;
case "B": price = 100; break;
case "C": price = 150; break;
}
}


alert ("The price is " + price);
}

assignPrices();

</script>

Note that JavaScript form validation only provides convenience for users, not security. This means that JavaScript should be used as an "enhancement", not as a requirement. So your form should not be dependent on JavaScript alone to assign the prices or perform your validation. Instead, whatever server-side language you use to process the form (PERL, ASP, PHP, etc.) should also perform the same validation. Otherwise, people will be able to bypass your validation (and even possibly inject malicious code) simply by disabling JavaScript.



This free kick is going to be a left-footed or a right-footed strike. - Football commentator

Last edited by Philip M; 09-02-2010 at 09:10 PM..
Philip M is offline   Reply With Quote