...

View Full Version : Validation/form submission problem



Local Hero
07-06-2005, 04:39 PM
I am trying to validate a form with three dropdown boxes. If the second two don't change when the user submits, and alert shows up. Right now, the alert shows up, but the form still submits. I don't know what's wrong.

<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<SCRIPT type="text/javascript">
function box() {
if (window.document.form_1.select_2.selectedIndex == 0 || window.document.form_1.select_X.selectedIndex == 0)
{
alert("Where do you think you're going?");
return false;
} else {
return true;
}
}

</SCRIPT>

</HEAD>

<BODY>
<form name="form_1" action="http://www.localheroclothing.com/store/customer/cart.php" method="post" onSubmit="box();">
<select name="select_1">
<option>-- Products --</option>
<option value=1>T-Shirt</option>
<option value=2>Longsleeve T-Shirt</option>
</select>
<select name="select_2">
<option>Please Select a Color</option>
<option>red </option>
<option>green </option>
</select>
<select name="select_X">
<option>Please Select a Size</option>
<option>Large</option>
<option>Small</option>
</select><INPUT type="submit" value="Add to Cart" >
</BODY>
</HTML>

If you'd like to try it, it will send you to my shopping cart, but not products will be added. The code looks fine to me, but something doesn't work. How do I keep it from submitting?

jaywhy13
07-06-2005, 04:58 PM
ok... found the problem... here's the corrected code. Exlpanation after:

<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
<SCRIPT type="text/javascript">
function box() {
if ((window.document.form_1.select_2.selectedIndex == 0 )||( window.document.form_1.select_X.selectedIndex == 0))
{
alert("Where do you think you're going?");
return false;
} else {
return true;
}

}

</SCRIPT>

</HEAD>

<BODY>

<form name="form_1" action="http://www.localheroclothing.com/store/customer/cart.php"
method="post" onSubmit="return box();">
<select name="select_1">
<option>-- Products --</option>
<option value=1>T-Shirt</option>
<option value=2>Longsleeve T-Shirt</option>
</select>
<select name="select_2">
<option>Please Select a Color</option>
<option>red </option>
<option>green </option>
</select>
<select name="select_X">
<option>Please Select a Size</option>
<option>Large</option>
<option>Small</option>
</select><INPUT type="submit" value="Add to Cart" >
</form>
</BODY>
</HTML>

Okay... now remember. In order for the form submission to be stopped... it has to resemble "return false"... when the "box()" executes... the only thing it returns is "false", so your submission thingy previously looked like.. onSubmit="false"... so sticking a "return" before the box() function solves the problem.

I tested it, it works!

Local Hero
07-06-2005, 05:17 PM
I'll try it..Thanks.

jaywhy13
07-06-2005, 05:23 PM
I'll try it..Thanks.
:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum