Hello all! I've been plugging away at this for longer then I'd like to admit and seem to have hit a barrier. It's a simple problem really, that is, it SHOULD be. I'm down to a few options (after hours of Google searches and trial-and-error), however I know there has to be a better way. I'm not really expereinced in Javascript, but understand the basics. Always willing to learn more.
Here's the scoop: I have multiple select forms (drop-down boxes) on a page. Each form is just that - a single select menu and a submit button. It goes to a CGI script that redirects the user to a different page depending on what they pick from the menu. (Part of a crude shopping cart system actually, the drop-down select menu is for size selection. Each different size of each product sends them to a different http link.)
All I'd like to do is have an alert box pop up and say "Please choose a size!" if they fail to before clicking the submit (add to cart) image. (It might make a difference I've read, so wanted to get that in there - the submit button is an image.) I've gotten the code I wrote to work on multiple levels, but never fully the way I think it should. I can use it easily to validate a SINGLE select form, but once I added multiple forms, I realized I needed to change it up a bit. So now I have multiple forms with multiple names, and a javascript function for each form. Being there can be up to 40 forms on a single page, it seems to get mess in the HEAD tag, being I will then have 40 functions, all doing the same thing. =/ This is my first problem... the second one I was having is trying to link the (working) code from an external .JS file.
There's the background info, here's the code in the HEAD tag:
... and it continue like that, changing the validate# and size# to however many forms I have on the page. (Up to 40 on some pages.) This in itself works, however i feel there has to be a better way, such as an array (The javascript n00b in me hinders me from knowing that?). I figure I should be able to use one function/array for all forms, but I couldn't figure out how.
The form is validated by a simple "onSubmit=validate#();" where # is different on each, calling one of those functions described above. My first question - IS THERE A BETTER WAY? =)
Second problem/question... if there isn't a better way, I'd like to link to all this code thru an external .JS file to save on page load. Seems sin\mple enough. I've read the format is to pop open a notepad document, copy all the actual code (minus script tags and such), then save it as a .JS file (like validate.js) and link it like:
The exact same code, but up to 99 functions, are included. I wanted to simple use this across all pages, linking to same same script. It simply doesn't work, even though it does if I put the code in the header. Linking to this JS file in the header just does nothing. Second question - AM I DOING SOMETHING WRONG? =)
I greatly appreciate any and all help on this. If I'm missing some info you need, by all means ask. Been frustrating me for some time, as I rarely run into a snag which a few hours and Google can't solve. TIA everyone, Happy Holiday's.
---------
~ GuidoZ
Here's the scoop: I have multiple select forms (drop-down boxes) on a page. Each form is just that - a single select menu and a submit button. It goes to a CGI script that redirects the user to a different page depending on what they pick from the menu. (Part of a crude shopping cart system actually, the drop-down select menu is for size selection. Each different size of each product sends them to a different http link.)
All I'd like to do is have an alert box pop up and say "Please choose a size!" if they fail to before clicking the submit (add to cart) image. (It might make a difference I've read, so wanted to get that in there - the submit button is an image.) I've gotten the code I wrote to work on multiple levels, but never fully the way I think it should. I can use it easily to validate a SINGLE select form, but once I added multiple forms, I realized I needed to change it up a bit. So now I have multiple forms with multiple names, and a javascript function for each form. Being there can be up to 40 forms on a single page, it seems to get mess in the HEAD tag, being I will then have 40 functions, all doing the same thing. =/ This is my first problem... the second one I was having is trying to link the (working) code from an external .JS file.
There's the background info, here's the code in the HEAD tag:
Code:
<script language="javascript">
function validate1()
{
if (size1.section.options[0].selected)
{
alert('Please choose a size before adding this item to your cart!');
event.returnValue=false;
}
}
function validate2()
{
if (size2.section.options[0].selected)
{
alert('Please choose a size before adding this item to your cart!');
event.returnValue=false;
}
}
function validate3()
{
if (size3.section.options[0].selected)
{
alert('Please choose a size before adding this item to your cart!');
event.returnValue=false;
}
}
</script>
... and it continue like that, changing the validate# and size# to however many forms I have on the page. (Up to 40 on some pages.) This in itself works, however i feel there has to be a better way, such as an array (The javascript n00b in me hinders me from knowing that?). I figure I should be able to use one function/array for all forms, but I couldn't figure out how.
The form is validated by a simple "onSubmit=validate#();" where # is different on each, calling one of those functions described above. My first question - IS THERE A BETTER WAY? =)
Second problem/question... if there isn't a better way, I'd like to link to all this code thru an external .JS file to save on page load. Seems sin\mple enough. I've read the format is to pop open a notepad document, copy all the actual code (minus script tags and such), then save it as a .JS file (like validate.js) and link it like:
Code:
<script language="JavaScript" src="validate.js" type="text/javascript"></script>
The exact same code, but up to 99 functions, are included. I wanted to simple use this across all pages, linking to same same script. It simply doesn't work, even though it does if I put the code in the header. Linking to this JS file in the header just does nothing. Second question - AM I DOING SOMETHING WRONG? =)
I greatly appreciate any and all help on this. If I'm missing some info you need, by all means ask. Been frustrating me for some time, as I rarely run into a snag which a few hours and Google can't solve. TIA everyone, Happy Holiday's.
---------
~ GuidoZ