Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    Jun 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onsbmit wont work?

    All i'm trying to do is run an onsubmit command and its not workin correctly. Here is what i have,

    in header,
    Code:
    <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,
    Code:
    <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!

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation

    doesn't return false... which would stop it from submiting:
    Code:
    <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>

  • #3
    New Coder
    Join Date
    Jun 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    New Coder
    Join Date
    Jun 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FYI, this might be the problem .. i don't know but i am using the following to submit the form,

    Code:
    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!

  • #5
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <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>
    Code:
    onClick="check_contact_name();"

  • #6
    New Coder
    Join Date
    Jun 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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)
    Code:
    <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.

  • #7
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    must be a syntax error then...
    ahh yes.. add the stuff in the blue.
    Code:
    <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>

  • #8
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <!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>

  • #9
    New Coder
    Join Date
    Jun 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •