Click to See Complete Forum and Search --> : what's wrong with this super simple form validator
sneakyimp
06-08-2004, 03:58 PM
i wrote this form with validator...everything works fine except the SUBMIT line...i get the error
Line: 14
Char: 2
Error: Object doesn't support this property or method
<HTML>
<BODY>
<FORM ACTION="search.php" NAME="jor_mudder" METHOD="POST">
<SCRIPT LANUGAGE="Javascript">
<!--
function SubmitAndValidate(myFormName) {
var myForm;
myForm = document.forms[myFormName];
alert(myFormName);
alert(myForm.name);
alert(myForm.userid.value);
myForm.submit();
}
//-->
</SCRIPT>
<INPUT TYPE="HIDDEN" NAME="submit" VALUE="1">
<INPUT TYPE="HIDDEN" NAME="username" VALUE="marcus">
<INPUT TYPE="HIDDEN" NAME="userid" VALUE="10">
<INPUT TYPE=BUTTON VALUE="submit" ONCLICK="SubmitAndValidate('jor_mudder');">
</FORM>
</BODY>
</HTML>
I've used this technique in the past and it has worked fine. Your help would be profoundly appreciated.
Pittimann
06-08-2004, 04:14 PM
Hi!
There are a couple of mistakes in your code. You could try something like that:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function SubmitAndValidate(myForm) {
alert(myForm.name);
alert(myForm.submitName.value);
alert(myForm.userid.value);
alert(myForm.username.value);
myForm.submit();
}
//-->
</SCRIPT>
</head>
<body>
<FORM ACTION="search.php" NAME="jor_mudder" METHOD="POST">
<INPUT TYPE="text" NAME="submitName" VALUE="1">
<INPUT TYPE="HIDDEN" NAME="username" VALUE="marcus">
<INPUT TYPE="HIDDEN" NAME="userid" VALUE="10">
<INPUT TYPE=BUTTON VALUE="submit" onCLICK="SubmitAndValidate(this.form);">
</FORM>
</body>
</html>The error message comes because of the fact, that you named one hidden field like the form. Only correcting that wouldn't make the validation work. And the above code is also not a validator!
Cheers - Pit
David Harrison
06-08-2004, 04:20 PM
You know, traditionally a submit button is used for submitting a form. Kinda makes sense don't you think?
sneakyimp
06-08-2004, 04:21 PM
Pit, you're AWESOME!
I've been staring at that code forever...yes i know it's not a validator...i had stripped out all the validation code trying to figure out why it wasn't working.
GO NOTEPAD (but wordpad's really where it's at...you can open larger files ).
THANKS SO MUCH
sneakyimp
06-08-2004, 04:24 PM
go venus!
I had tried using a plain old submit button before and using the onSubmit event to validate the form, but had real trouble. I don't recall the particulars but it was a pain.
If there is a better way, I'd be happy to see it. this is working for me now:
<HTML>
<BODY>
<FORM ACTION="search.php" NAME="jor_mudder" METHOD="POST">
<SCRIPT LANUGAGE="Javascript">
<!--
function SubmitAndValidate(myFormName) {
var myForm = document.forms[myFormName];
alert(myForm.name);
alert(myForm.userid.value);
myForm.submit();
}
//-->
</SCRIPT>
<INPUT TYPE="HIDDEN" NAME="submit_attempt" VALUE="1">
<INPUT TYPE="HIDDEN" NAME="username" VALUE="marcus">
<INPUT TYPE="HIDDEN" NAME="userid" VALUE="10">
<INPUT TYPE=BUTTON VALUE="submit" ONCLICK="SubmitAndValidate('jor_mudder');">
</FORM>
</BODY>
</HTML>
Pittimann
06-08-2004, 04:26 PM
Hi lavalamp!You know, traditionally a submit button is used for submitting a form. Kinda makes sense don't you think?Asking me! If so: I agree, what else?
Have a good night (evening)!
Pit
Edit: saw your latest post too late and have to say: this cannot work. Just have a look at the file, lavalamp attached.
David Harrison
06-08-2004, 04:28 PM
Originally posted by sneakyimp
If there is a better way, I'd be happy to see it.I've already posted one.Originally posted by Pittimann
Hi lavalamp!Asking me! If so: I agree, what else?I didn't know that you had posted until I hit submit, but now you come to mention it, why didn't you use a submit button?
Pittimann
06-08-2004, 04:33 PM
Hi again, lavalamp!why didn't you use a submit button?I made the code originally posted do, what sneakyimp seemed to want this code to do and mentioned, that this is not a "validator".
Greetz - Pit
David Harrison
06-08-2004, 04:41 PM
Fair enough Pit, but if you think that you can be improve someone's code then by all means do it. After all, the worst thing that can happen is that they won't like it.
sneakyimp, since you said you had some trouble last time, here's a couple of newer versions of the code with some simple validations in them.
sneakyimp
06-08-2004, 05:22 PM
you guys continue to rock....thanks.
i've tried the form onsubmit thing before and had problems...i can't for the life of me recall what problems i had...it might have had something to do with the modularity of my PHP code or something completely outside of javascript.
that said, it appears as though it will work in my current context...and it does look like better form.
THANKS.
David Harrison
06-08-2004, 05:32 PM
Glad to have helped. :)
Edit.