...

View Full Version : onsbmit wont work?



IronCode
11-11-2004, 12:17 AM
All i'm trying to do is run an onsubmit command and its not workin correctly. Here is what i have,

in header,


<script language="JavaScript">
<!--
function check_contact_name(){
valid = true;
if( document.contact_form.contact_first_name == ""){
alert ("Please fill in the 'First Name' field.");
valid = true;
}
return valid;
}
//-->
</script>


and this is in the form tag,


<form method="post" name="contact_form" action="file.html" onSubmit="return check_contact_name();">


it should work shouldn't it? i mean i'm not very good at javascript but that looks about right lol

any help would be great, thx guys!

fci
11-11-2004, 12:22 AM
doesn't return false... which would stop it from submiting:

<script language="JavaScript">
<!--
function check_contact_name()
{
var form = document.contact_form;
if (form.contact_first_name=="") {
alert ("Please fill in the 'First Name' field.");
form.contact_first_name.focus()
return false;
}
return true;
}
//-->
</script>

IronCode
11-11-2004, 02:12 AM
thanks for the reply, i cutt and pasted your code over mine. Double check the naming and the onsubmit command in the form tag. But it still don't work :(

Not sure whats going on, i can do a manual submit verify and spit them back but thats a lot of work for something JS can do in a second.

Any help would be great!

IronCode
11-11-2004, 02:21 AM
FYI, this might be the problem .. i don't know but i am using the following to submit the form,



onClick="javascript: document.contact_form.submit();"


the form is dynamically filled using PHP and MySQL. pretty much most of the content and form elements are generated through those. The images that i use to submit the form change and the only why i could get them to all work in the order they are supposed to was to use that onclick statement to submit the form.

The reasoning is that one of the 4 submit buttons has multiple actions based on what form is loaded. So one might be leading to another page if one variable is passed but that same image link has a completely different effect is a different variable is passed.

Although all 4 images are submit buttons in this p[articualr case, however 2 of them submit and redirect at the same time.

Kinda hard to explain but everything works flawlessly except this lol.

Thx again for your help guys!

fci
11-11-2004, 02:27 AM
<script language="JavaScript">
<!--
function check_contact_name()
{
var form = document.contact_form;
if (form.contact_first_name=="") {
alert ("Please fill in the 'First Name' field.");
form.contact_first_name.focus()
return false;
}
form.submit();
return true;
}
//-->
</script>


onClick="check_contact_name();"

IronCode
11-11-2004, 02:32 AM
pasted your code over mine, including the onclick but it still submits the form without ever fire off the function :(

here is one of the images, (after parsed)


<a onMouseOut="MM_swapImgRestore();" onMouseOver="MM_swapImage('contact_r1_c8','','images/contact_r1_c8_f2.jpg',1);" onClick="check_contact_name();" class="hand"><img name="contact_r1_c8" src="images/contact_r1_c8.jpg" width="87" height="28" border="0" alt=""></a>


You see i have some JS runnin fine as the rollovers seem to work great.

fci
11-11-2004, 02:35 AM
must be a syntax error then...
ahh yes.. add the stuff in the blue.


<script language="JavaScript">
<!--
function check_contact_name()
{
var form = document.contact_form;
if (form.contact_first_name.value=="") {
alert ("Please fill in the 'First Name' field.");
form.contact_first_name.focus()
return false;
}
form.submit();
return true;
}
//-->
</script>

hemebond
11-11-2004, 02:38 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>47323</title>
</head>
<body>
<form id="contact_form" name="contact_form" method="post" action="file.html" onsubmit="return validate()">
<fieldset>
<label for="contact_name">Contact Name</label>
<input id="contact_name" name="contact_name" type="text" value="">
<input type="submit">
</fieldset>
</form>
</body>
<script type="text/javascript">
function validate()
{
if( document.getElementById("contact_name").value == '')
{
alert ("Please fill in the 'First Name' field.");
return false;
}
return true;
}
</script>
</html>

IronCode
11-11-2004, 02:41 AM
WOOT! thx so much man, FCI your fix did the job perfectly!

i didn't even get to try the other solution but thx so much for your help guys!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum