|
Hi there,
I'm going nuts..the validation part of my form works, but then I also want the submitter to go to a thank you page or get a thanks pop-up, and then it stops working..
When using only 1 of the 2 it works fine, but I need both I think..
What it does when using below: it validates but sends anyway after clicking ok whether someone entered something or not AND does not go to the url..
The script in the heading=
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
}
function MM_goToURL() { //v3.0
var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
for (i=0; i<(args.length-1); i+=2) eval(args+".location='"+args[i+1]+"'");
}
And the script on the submit button =
<input type="submit" value="Send" onClick="MM_validateForm('Name','','R','E-mail','','RisEmail','Comments','','R')" onSubmit="MM_goToURL('parent','contact.htm');return document.MM_returnValue">
|
|
|
Interesting....your first function that you fire onclick of the submit button validates the form. Your second function, that you attempt to fire onsubmit, is quite odd to me.
A submit button does not have an onsubmit event, but a form does. So you want to put your onsubmit event handler within your <FORM> tag.
I have to assume that your form is within a FRAME or IFRAME because you are attempting to submit this form but you are also attempting to redirect the parent to a new location (contact.htm). The problem here is that whether the form is in a FRAME or an IFRAME, if you redirect the parent (the window object that contains the current window), you will lose the ability to submit the form since the form will no longer exist as the parent navigates. If you are not using FRAMES or IFRAMES, the same problem exists since parent = self. You can't navigate the window that contains the form, then expect to submit the form -- or vice versa for that matter.
Another note is that the MM_goToURL() function (no doubt adapted from Macromedia generated code) is strange code if all you want to do is pass a window object and a url to navigate to. Why the for() loop? Why not simply:
function MM_goToURL(winObj,url) {
winObj.location = url;
} |
|
Give this some more thought and see if you can't accomplish your goal. If you need more help, just post a concise, detailed reply telling us what you want to do.
|
|
|
|
|
Hi,
Thanks a lot for your reply, to add the script to the form tag instead of the submit was an idea that also occurred to me after some hours, but actually my wife helped me out with adding an 'else' function to the first script, basically by first having the script check the validation, and if all is ok to submit and go to a new page.
And it works like a charm :-)
All it took was adding 3 lines: function Send()
{document.location='/forum/thanks.htm';}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) {alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '')} else {Send()};
That was it, and this took me 8 hours.....
Thanks very much anyway,
Heyme
|
|
|
|
|
|
|
|