PDA

View Full Version : Function to call function


dremmers
11-02-2002, 12:04 PM
Sigh. I'm so confused.

On submit, I am running the following function:

function noEntry()
{
mt=document.form.email.value;
if ((mt.length<1)||(mt.substring(0,6)=="******")) {
alert("To add this story to your ebook, you must include a valid email address.");
document.form.email.focus();
return false;
}
else { return true; }
}

Now where do I tell it to go on to the next function (thankYou -- which builds a dynamic thank you page after the form has been submitted)? :confused:

Thanks for your help...

mordred
11-02-2002, 12:51 PM
Ehrr... if you submit your form, isn't the URL specified in the action attribute loaded, and shouldn't that be exactly your "Thank You" page? I mean, if a form is already submitted, you can't invoke JS functions on this page any longer, because the new one should come (which of course can be the same page though).

Perhaps you clarify your request so that we get a better understanding what exactly you want to achieve. I didn't get it. :)

dremmers
11-02-2002, 01:26 PM
No, the thank you page is dynamic. That is, it shows the user what they entered on the form, so I need the thankYou function to pick up the form entries and open the file and place the information in the correct spots.

Here are the two functions. I need to link them together so that after noEntry is run, if everything is OK, it goes on to thankYou.

---------

function noEntry()
{
mt=document.form.email.value;
if ((mt.length<1)||(mt.substring(0,6)=="******")) {
alert("To add this story to your ebook, you must include a valid email address.");
document.form.email.focus();
return false;
}
else { return true; }
}

AND

function thankYou(form)
{
form.redirect.value = 'http://www.mylifestory.org/ThankYou.html' + '?' + escape('Written by') + '=' + getText(form.realname) + '&' + escape(question) + '=' + getText(form.Story) + '&';

if (form.AddToBook.checked == false)
{
location.href = form.redirect.value;
return false;
}
return true
}

dremmers
11-02-2002, 08:09 PM
OK, so it seems to me that I should just be able to call the function thankYou somewhere in the noEntry else section, but how exactly do I do this? I can't seem to just type in the name of the function, are my returns screwed up somewhere?

Argh....

mordred
11-03-2002, 10:48 AM
Normally you should be able to do:


function noEntry()
{
mt=document.form.email.value;
if ((mt.length<1)||(mt.substring(0,6)=="******")) {
alert("To add this story to your ebook, you must include a valid email address.");
document.form.email.focus();
return false;
}
else {
thankYou(document.form);
return true;
}
}


Perhaps your form field names were screwed up, who knows without seeing the HTML form?

P.S: 'the thank you page is dynamic' can mean almost everything. It could also be that it is a PHP, ASP, JSP, .... page that puts together a 'thank you' page dynamically on the server side. I understood you now that you want to do this entirely on client-side.

dremmers
11-04-2002, 10:57 AM
THAT'S IT!!!!!!

I wasn't passing document.form as the argument to thankYou.

thankYou!! thankYou!! thankYou!! thankYou!!

I LOVE this forum!!!!:D

dremmers
11-04-2002, 11:34 AM
Argh. One more thing...I promise this is the LAST question on this!

I am now trying to make noEntry and thankYou part of an if statement, but it only half works!

Here are my results:
1. if AddToBook is not checked, it goes to thankYOU
2. if AddToBook is checked AND form.email is empty, I get an alert
3. BUT if AddToBook is checked AND form.email is filled in, NOTHING HAPPENS WHEN I SUBMIT??????

Here is the code:

function ebookAdd() {
if (form.AddToBook.checked == true)
{
noEntry();
return false;
}
else
{
thankYou(document.form);
return true;
}
}

function noEntry()
{
mt=document.form.email.value;
if ((mt.length<1)||(mt.substring(0,6)=="******"))
{
alert("To add this story to your ebook, you must include a valid email address.");
document.form.email.focus();
return true;
}
else
{
thankYou(document.form);
return false;
}
}

function thankYou(form) {
form.redirect.value = 'http://www.mylifestory.org/ThankYou.html' + '?' +
escape('Written by') + '=' + getText(form.realname) + '&' +
escape(question) + '=' + getText(form.Story) + '&';

if (form.AddToBook.checked == false)
{
location.href = form.redirect.value;
return false;
}
return true;
}

mordred
11-04-2002, 06:09 PM
I suppose the line in bold is the source of your confusion:


function thankYou(form) {
form.redirect.value = 'http://www.mylifestory.org/ThankYou.html' + '?' +
escape('Written by') + '=' + getText(form.realname) + '&' +
escape(question) + '=' + getText(form.Story) + '&';

if (form.AddToBook.checked == false)
{
location.href = form.redirect.value;
return false;
}
return true;
}


Shouldn't that rather be

if (form.AddToBook.checked == true)

because this is the same precondition you tested before calling this function - and you want the checkbox to be checked, no?

P.S: It doesn't matter how much questions you ask about your script. That's what a discussion forum is meant for. Just don't get intimidated when someone shows a "Doh-how-simple-it-was" answer... we all learn something new everyday. :)

dremmers
11-04-2002, 08:01 PM
LOL! I didn't say I would stop posting...I just said I would stop posting on this issue! Ha ha. I just get frustrated when I think a particular issue is resolved and it turns out it's not. :o

Anyway, the line you bolded actually works exactly the way I want it to. To give you a little more background, here's what my page does in English:

1. Users fill out a form. When they submit, the contents of their form are returned to them on an html "thank you" page (nothing else happens...that's why the condition is false). End.

OR

2. IF they also check the box, then when they submit, they get the "thank you" page AND the contents of the form are emailed to me via a Perl script (the condition to send email is true). End.

So, if they check the box, I also need them to include their address so that I can email back to them. Checking the email field is only necessary if the box is checked . But this is where I am getting into trouble.

I started by ALWAYS checking to see if there was an email address (whether or not the box was checked) and verifying that I could do this first and then go on to the thankYou routine. Thanks to your help, that works perfectly.

Now, however, I want to add the final piece, which is to make the noEntry function (which leads into the thankYou function) conditional on the checkbox. If it's checked, go to noEntry. Once there, stop if there is no email address or go on to thankYou if there is. If it's not checked, go straight to thankYou.

With the funtions I posted previously in place, I only have a problem if both the box is checked AND there is an email address.

When this is the case, clicking submit just does nothing.

So, if you would, please look at the code again and see if you notice anything wrong or missing based on what I have described.

It must be failing in the noEntry else portion, because I am not getting the alert, which I do if the field is empty.:confused:

Thanks in advance...

whammy
11-05-2002, 12:39 AM
if ((mt.length<1)||(mt.substring(0,6)=="******") && form.AddToBook.checked == true){
alert('Er, there is a problem');
}

Should be your answer, I think... I didn't look through your code completely, but assuming you include the correct form references, I think that's what you're looking for.

By the way, that was a very good explanation of your problem. People should take lessons from you in how to explain their scripting problems. :)

P.S. I'm not sure what you're doing with the "******" since I've never used any email validation like that! - but if it works, so be it. Email validation is dodgy at best anyway unless you have a server-side sniffer to see if the email address actually exists.

:)

dremmers
11-05-2002, 12:13 PM
Thank you! I may be clear, but now I am sooooo confused.

I don't think I can use the &&, because it doesn't check for the condition when AddToBook is NOT checked...in which I need to run a different routine.

I did discover, however, that the three conditions I described were not working after all...I was still getting the email, whether or not the box was checked, which means for some reason it wasn't reassigning the redirect. Anyway, I made some changes and here is my new function:

function ebookAdd() {
if (form.AddToBook.checked == false)
{
thankYou(document.form);
return false;
}
else
{
noEntry();
return false;
}
return true;
}


And just for reference, here are the other two again:

function noEntry() {
mt=document.form.email.value;
if ((mt.length<1)||(mt.substring(0,6)=="******"))
{
alert("To add this story to your ebook, you must include a valid email address.");
document.form.email.focus();
return false;
}
else
{
thankYou(document.form);
return true;
}
}

function thankYou(form) {
form.redirect.value = 'http://www.mylifestory.org/ThankYou.html' + '?' +
escape('Written by') + '=' + getText(form.realname) + '&' +
escape(question) + '=' + getText(form.Story) + '&';

if (form.AddToBook.checked == false)
{
location.href = form.redirect.value;
return false;
}
return true;
}


My problem is definitely related to the returns in the ebookAdd function when I call noEntry(). If I have "return false", then after the alert my field is focussed and ready to add an email. BUT, after I do, clicking Submit does nothing.

If, however, I put "return true", then after the alert, my field is focussed and it goes on to send the email -- WITHOUT WAITING FOR ME TO ENTER TEXT IN THE FIELD.

Did I mention I am confused? How can I get it to stop if there is no address, but go on if there is? What am I missing?

Stressed out in Virginia...and I'm not even running for office!

P.S. Truth be told, I don't know what the ****** does either. It was just part of the script I pilfered.

dremmers
11-05-2002, 08:14 PM
SUCCESS!!!!!!!!!!!!!!!!!!!!!!

WOO HOO!!!!!!!!!!!!!!!!

Though your suggestions didn't directly factor into the result, I AM SO GRATEFUL for your help and the chance to "talk" this all out.

I love this forum!!!!!!

So here's the resolution to my problem. It's amazing how time away from something gives your mind a chance to just sift through things...

Anyway, I was driving around chewing on the issue when I realized that something wasn't quite right about the fact that I was checking the value of AddToBook twice, once to see if it was checked and another to see if it wasn't, in two separate functions. That seemed sloppy. Shouldn't I just do x if it's checked and do y if it's not?

Then it hit me. In thankYou, I was already saying, "if it's not checked, do this". ALL I HAD TO ADD was "if it's not, do that". I just needed to add an else! So I combined all three functions into one and VOILA...Perfection!

FYI, here is my final, very simple, combined function.

function thankYou(form)
{
form.redirect.value = 'http://www.mylifestory.org/ThankYou.html' + '?' +
escape('Written by') + '=' + getText(form.realname) + '&' +
escape(question) + '=' + getText(form.Story) + '&';

if (form.AddToBook.checked == false)
{
location.href = form.redirect.value;
return false;
}
else
{
mt=document.form.email.value;
if ((mt.length<1)||(mt.substring(0,6)=="******"))
{
alert("To add this story to your ebook, you must include a valid email address.");
document.form.email.focus();
return false;
}
return true;
}
return true;
}


Ahh... isn't life great?

Thanks again for all your help!!!!!!!!!!:thumbsup:

whammy
11-05-2002, 11:37 PM
Cool, actually I was going to suggest that after your second to last post, but it looks likeyou figured it out. :cool: