/    Sign up×
Community /Pin to ProfileBookmark

Problem with multiple includes

Hi, I’m relatively new to javascript – so I’m hoping this is a real newbie error. But I’ve tried lots and cant fathom it out.

ok, so I have saved off all my javascript to two .js files. The first is very much a set of form validation functions. The second is just a function with a case statement in.

We have a html page listing a bunch of options in a drop down selection box.

On submit, a function called Validate is called. This function is in js 1. It checks to make sure the user has entered a name, address etc… and then it comes to the final check, that of the option box. Now for any option selected, three variables have to be assigned, like retURL as well as few database specific ones.

So once all the easy validation stuff is done, I call a function that sits in the other js file. Now if I just use the one js file, it all works, if I try to use two – the second function doesn’t seem to pick up the form variable.

Here is a simplified version of the two sets of code:

In js one:

[CODE]
function validate()
{
with (document.webform)
{
var alertMessage = “Please enter:n”;

// USER VALIDATION
if (Campaign_ID.value == “”) alertMessage += “nChoose Item”;
if (name.value == “”) alertMessage += “nName”;
if (address.value == “”) alertMessage += “nAddress”;
if (email.value == “”) alertMessage += “nEmail”;

// DISPLAY ALERT
if (alertMessage != “Please enter:n”)
{
alert(alertMessage);
return (false);
}

campaign();

return (true);
}
}
[/CODE]

and here is the second js file in it’s entirety:

[CODE]
function campaign()
{
switch(Campaign_ID.value)
{
case ‘1234567890’:
elements(“0000000000XXX”).value=”Item X chosen”;
elements(“0000000000YYY”).value=”Item X chosen”;
elements(“retURL”).value=”http://www.website.com”;
break;
}
}
[/CODE]

So on the actual web form, there is a drop down selection window, where you can choose from a number of items. I have just listed one above, but there will be lots. I’m just testing it with one currently. The value of the selection would be set to that 10 digit code seen in the case expression.

Where am I going wrong? I know the second js file gets called because I stuck an alert in there before the case expression. See, in the first js file, if I do something like:

alert(elements(“Campaign_ID”).value);

I get the correct value displayed on screen, but if I put that same alert before the case expression in the second file, it errors and nothing happens.

Now I know I could just stick it all in one js file and be done with it, but there maybe other js files at some point with more and more campaigns for different areas. Plus I would just like to know why it doesn’t work, it’s bugging me now ?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@tarsusOct 12.2006 — I don't think the problem is with having two JS files. It looks like your campaign() function doesn't know anything about the variable Campaign_ID. In your function call, try:

[CODE]campaign(Campaign_ID);[/CODE]

And include it as an argument in your function definition:

[CODE]function campaign(Campaign_ID) {[/CODE]
Copy linkTweet thisAlerts:
@alaspillowsauthorOct 13.2006 — thanks for the reply, although that didn't work either I'm sorry to say...

When you said that, I couldn't fathom out why one function can happily see the variables from the web form, and this one should have to have them passed to it. It's then when I spotted that my validate function had the

with (document.webform){
I added that to my second function, and after a bit of cheesing around got it to work finally... phewf, I'll be able to relax this weekend now.

Thanks again
×

Success!

Help @alaspillows spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...