View Full Version : onsubmit help (Can you do 2 functions?)


StyGomez
02-24-2003, 12:37 AM
I'm just doing a simple e-mail form using php. Everything works fine except I found two great scripts from www.dynamicdrive.com that I'd like to use... The first check to make sure all the fields are filled in and the second makes it so the submit button can only be pressed once.

So, of course, they both start with "onsubmit" tags and I can only get them to work one at a time. If I put both, only the first one works. But they work fine individually.

Is there a way I can get them both working at the same time? Renaming or something like that? :) I'll supply the code if you want but I didn't think it was necessary. :P

pb&j
02-24-2003, 04:02 AM
yes, it does help to post the coding and/or the page in question.

it is just a guess that both things are being routed to a function. you could either meld both functions together or try putting both calls into the onsubmit being separated by a semicolon.

Dude128
02-24-2003, 04:15 AM
what about creating another function that calls both the other two, and calling that one using onsubmit?

StyGomez
02-24-2003, 12:04 PM
Alright, here's the code for the whole page (to make sure I've done it right). I highlighted the javascripts in question. pb&j and dude: Could you show me an example of what you mean? I'm not reallly familiar with javascript :)

<html><head><title>main</title>

<link rel="stylesheet" href="sheeta.css" type="text/css">

<SCRIPT LANGUAGE="JavaScript">
<!-- Original: CodeLifter.com (support@codelifter.com) -->
<!-- Web Site: http://www.codelifter.com -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
if (parent.location.href == self.location.href) {
// change the url below to the url of the
// frameset page...
window.location.href = 'frames.html';
}
// End -->
</script>

<script language="JavaScript">
<!--

/*
Required field(s) validation- By NavSurf
Visit NavSurf.com at http://navsurf.com
Visit http://www.dynamicdrive.com for this script
*/

function formCheck(formobj){
//1) Enter name of mandatory fields
var fieldRequired = Array("email", "realname", "comments");
//2) Enter field description to appear in the dialog box
var fieldDescription = Array("Your E-mail", "Your Name", "Comments");
//3) Enter dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
//-->
</script>

<script>

/*
Submit Once form validation-
© Dynamic Drive (www.dynamicdrive.com)
For full source code, usage terms, and 100's more DHTML scripts, visit http://dynamicdrive.com
*/

function submitonce(theform){
//if IE 4+ or NS 6+
if (document.all||document.getElementById){
//screen thru every element in the form, and hunt down "submit" and "reset"
for (i=0;i<theform.length;i++){
var tempobj=theform.elements[i]
if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
//disable em
tempobj.disabled=true
}
}
}
</script>

</head>
<body bgcolor="#FFFFFF" background="main.jpg" bgproperties="fixed" >

<font face="verdana" style="font-size:11px;">

<form method="post" action="/feedback.php" onsubmit="return formCheck(this)" onSubmit="submitonce(this)">
<table border=0 width="80%">
<tr>
<td><font face="verdana" style="font-size:11px;">Your E-Mail:</font></td>
<td><input type="text" name="email" size="10"></td>
</tr>
<tr>
<td><font face="verdana" style="font-size:11px;">Your Name:</font></td>
<td><input type="text" name="realname" size="10"></td>
</tr>
<tr>
<td valign="top"><br><font face="verdana" style="font-size:11px;">Comments:</font></td>
<td><textarea name="comments" wrap=physical rows=5 cols=20></textarea><br>&nbsp;<br></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="Send to Ann" style="background:#000000"style="color:#FFFFFF" border-width: 1px style="font-family:verdana"

style="font-size:11px;"></>&nbsp;&nbsp;<input type="reset" value="Start Over" style="background:#000000"style="color:#FFFFFF" border-width: 1px

style="font-family:verdana" style="font-size:11px;">
<br>
<font size=1>Powered by <a href="http://www.thesitewizard.com/" target="_blank">thesitewizard.com</a></font>
</td>
</tr>
</table>
</form>

<br><br><br>

<a href="javascript:history.go(-1)">[back]</a>

</body></html>

StyGomez
02-24-2003, 12:57 PM
Sorry, it won't let me edit my post because it's been more than 5 min.

Anyway, here's what I tried:

<form method="post" action="/feedback.php" onsubmit="return formCheck(this);submitonce(this)">

Again, the first code worked but the second didn't.


EDIT:

But I just have to say, I FIGURED IT OUT! I changed the code to this:

onsubmit="return ( formCheck(this) && submitonce(this) );"

WHICH WORKED! I found the answer here:

http://www.codingforums.com/showthread.php?threadid=7717&highlight=onsubmit

But thanks for trying to help me :)