lessdangerous
06-23-2005, 09:27 PM
I'm putting together a form that'll send info to my shopping cart script. If the user for some reason doesn't complete the three fields in the form, I want an alert pop up and PREVENT the form from submitting. You can see what I've set up below and it all works except that when a field is left incomplete, the alert will come up but after clicking OK the form is still submitted anyway. So, I can't figure out what I'm doing wrong.
I'm pretty unfamiliar with javascript but mostly understand it if I'm reading it. However, I'm not sure why the script is working the way it is.
Please take a look at my (simplfied) code and tell me what I'm doing wrong.
<head>
<script type="text/javascript">
function check(){
if(document.forms[0].QUANTITY.value==''){
alert("You have left a field empty.");
return false;
}
}
</script>
<script><!--
function getPrice(formName){
var myForm = formName.name;
var COLOR = formName.INFO;
var SIZE = formName.INFO2;
var SUBPRICE;
var COLORPRICE;
var SIZEPRICE;
var PRICE = formName.PRICE;
if ( myForm == "polo" ){
if ( COLOR.value == "White" ){
COLORPRICE = "15.95";
}
if ( SIZE.value == "XL" ){
SIZEPRICE = "1.00";
}
}
SUBPRICE = parseFloat(COLORPRICE) + parseFloat(SIZEPRICE);
PRICE.value = (SUBPRICE + "");
}
// -->
</script>
</head>
<body>
<form name="polo" onSubmit="return AddToCart(this)" action="managecart.html">
<select name="INFO">
<option value="" selected> </option>
<option value="White">White</option>
</select>
<select name="INFO2">
<option value="" selected> </option>
<option value="XL">XL</option>
</select>
<input name="QUANTITY" type=text onChange='this.value=CKquantity(this.value)'>
<input type="image" src="button.gif" onClick="check(); getPrice(this.form)" value="">
</form>
</body>
I'm pretty unfamiliar with javascript but mostly understand it if I'm reading it. However, I'm not sure why the script is working the way it is.
Please take a look at my (simplfied) code and tell me what I'm doing wrong.
<head>
<script type="text/javascript">
function check(){
if(document.forms[0].QUANTITY.value==''){
alert("You have left a field empty.");
return false;
}
}
</script>
<script><!--
function getPrice(formName){
var myForm = formName.name;
var COLOR = formName.INFO;
var SIZE = formName.INFO2;
var SUBPRICE;
var COLORPRICE;
var SIZEPRICE;
var PRICE = formName.PRICE;
if ( myForm == "polo" ){
if ( COLOR.value == "White" ){
COLORPRICE = "15.95";
}
if ( SIZE.value == "XL" ){
SIZEPRICE = "1.00";
}
}
SUBPRICE = parseFloat(COLORPRICE) + parseFloat(SIZEPRICE);
PRICE.value = (SUBPRICE + "");
}
// -->
</script>
</head>
<body>
<form name="polo" onSubmit="return AddToCart(this)" action="managecart.html">
<select name="INFO">
<option value="" selected> </option>
<option value="White">White</option>
</select>
<select name="INFO2">
<option value="" selected> </option>
<option value="XL">XL</option>
</select>
<input name="QUANTITY" type=text onChange='this.value=CKquantity(this.value)'>
<input type="image" src="button.gif" onClick="check(); getPrice(this.form)" value="">
</form>
</body>