Click to See Complete Forum and Search --> : having trouble calling 2 functions on submit


halcali
05-14-2004, 12:40 AM
i read online that seperating both function with a ';' would work... but the following does not....

<input type="submit" value="SUBMIT RESULTS" onClick="return verify();return verify2();">


basically with the above whichever function is first gets called, the other gets ignored. this is the code that it is calling (its a pop up alert to make sure all questions are answered)....

<script>
<!--
answers=new Array(48);
for (i=1; i<=48; i++) {answers[i]=0;}
function verify()
{
var i, found=0, error="";
for (i=1; i<=48; i++)
{
if (answers[i]==0)
{
if (error>"") {error=error+", ";}
error=error+i;
found++;
}
}
if (found==1) {alert("You missed question #"+error+"!"); return false;}
if (found>1) {alert("You missed "+found+" questions: "+error+"!"); return false;}
}




//-->
</script>

<script>
<!--
answersB=new Array(48);
for (i=1; i<=48; i++) {answersB[i]=0;}
function verify2()
{
var i, found=0, error="";
for (i=1; i<=48; i++)
{
if (answersB[i]==0)
{
if (error>"") {error=error+", ";}
error=error+i;
found++;
}
}
if (found==1) {alert("You missed question #"+error+"!"); return false;}
if (found>1) {alert("You missed "+found+" questions: "+error+"!"); return false;}
}
//-->
</script>

any ideas on how to get both functions to get called when pressing submit or alternatively integrating both functions into one function?

Kor
05-14-2004, 02:23 AM
as a general way, one solution may be

<form onsubmit="return verify()">

<script>
function verify(){
if(){.....;return false}
else{verify2()}
}
function verify2(){
if(){.....;return false}
else{return true}
}
</script>

But in your case you may combine scripts and make a single one, as you have same arrays length.

By the way... I think there is a mistake. If you have 48 elements in arrays, the limit should be i<48 not i<=48.

And... if you don't want to pass i as a general variable, better let it be a local one, to avoid possible conflicts
for(var i=0;.....

halcali
05-14-2004, 10:15 AM
i experimented a little with combining them, but i could not get it to work. do you have any ideas on how to combine the two (both functions are above)?

neil9999
05-14-2004, 10:20 AM
Try this:

<input type="submit" value="SUBMIT RESULTS" onClick="return (verify()&&verify2())">

Neil

halcali
05-14-2004, 10:48 AM
i tried that and it ignores the second function.

neil9999
05-14-2004, 10:51 AM
Works with me, try editing this:

<script type="text/javascript">
function verify(){
return document.getElementById("bx1").checked
}
function verify2(){
return document.getElementById("bx2").checked
}
</script>
</head>

<body>
<form action="http://www.google.co.uk">
<input type="checkbox" id="bx1">
<input type="checkbox" id="bx2">
<input type="submit" value="SUBMIT RESULTS" onClick="return (verify()&&verify2())">
</form>

Remember to remove any new lines generated by the forum.

Neil

halcali
05-14-2004, 11:18 AM
this is what i have now...

<script>
<!--
answers=new Array(48);
for (i=1; i<=48; i++) {answers[i]=0;}
function verify()
{
var i, found=0, error="";
for (i=1; i<=48; i++)
{
if (answers[i]==0)
{
if (error>"") {error=error+", ";}
error=error+i;
found++;
}
}
if (found==1) {alert("You missed question #"+error+"!"); return false;}
if (found>1) {alert("You missed "+found+" questions: "+error+"!"); return false;}
}


//-->
</script>

<script>
<!--
answersB=new Array(48);
for (i=1; i<=48; i++) {answersB[i]=0;}
function verify2()
{
var i, found=0, error="";
for (i=1; i<=48; i++)
{
if (answersB[i]==0)
{
if (error>"") {error=error+", ";}
error=error+i;
found++;
}
}
if (found==1) {alert("You missed question #"+error+"!"); return false;}
if (found>1) {alert("You missed "+found+" questions: "+error+"!"); return false;}
}
//-->
</script>



<input type="submit" value="SUBMIT RESULTS" onClick="return (verify()&&verify2())">

only the first listed function works. if i reverse the order, the same, whichever is first works, the second function gets ignored.