onSubmit and javascript dont work

Discussion in 'JavaScript' started by userman, Nov 16, 2010.

  1. #1
    Hi,

    Who can tell me why this dont work?

    <script language="JavaScript">
    function fecha(){
    fecha = document.getElementById('C7');
    if(fecha.value == ""){
    alert('You must indicate the approximate date of arrival');
    return false;
    }
    </script>
    
    <form id="formulario" method="post" name="formulario" action="result.php" onSubmit="return fecha()">
    <input name="C7" type="text" id="dateArrival" size="10">
    <input id="send" name="send" type="submit" value="Submit" />
    
    Code (markup):
    regards.
     
    userman, Nov 16, 2010 IP
  2. imocoder

    imocoder Member

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    There are 2 errors:
    - the C7 input name ID should be C7 not dateArrival
    - you forgot to put a closing bracket for the function

    
    <script language="JavaScript">
    function fecha(){
    fecha = document.getElementById('C7');
    if(fecha.value == ""){
    alert('You must indicate the approximate date of arrival');
    return false;
    }
    }
    </script>
    
    <form id="formulario" method="post" name="formulario" action="poll.php" onSubmit="return fecha()">
    <input name="C7" type="text" id="C7" size="10">
    <input id="send" name="send" type="submit" value="Submit" />
    
    Code (markup):
     
    imocoder, Nov 16, 2010 IP
  3. userman

    userman Active Member

    Messages:
    158
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Last edited: Nov 16, 2010
    userman, Nov 16, 2010 IP
  4. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #4
    you should use jslint to check syntax problems:

    Error:
    Problem at line 2 character 5: 'fecha' is a function.
    fecha = document.getElementById('C7');
    Problem at line 3 character 21: Use '===' to compare with ''.
    if (fecha.value == "") {
    Problem at line 1 character 18: Unmatched '{'.
    function fecha() {
    Implied global: alert 4
    Code (markup):
    This is a really bad bad example for a number of reasons, good thing you posted it so ppl can learn from it.

    when you define a function in javascript, eg. `function foo() { ... some code ... }', what the interpreter does is in fact:

    
    var foo;  // (hoisting)
    ...
    foo = function() {
        // some code
    };
    
    Code (javascript):
    variable declarations are appended to the window / global object (unless in a scope) so they become available via window.foo(); or even window['foo']() (which can be used to call functions dynamically from variables).

    what you also need to know is that javascript has a scope chain - this means, variables are declared at whatever level and inherited further down. hence, variables declared outside of all functions are becoming global (also available via window.varname)

    when you declare a variable inside a function, you HAVE to say 'var myname = 'dimitar';'

    failure to prefix this with var results in javascript declaring myname as a global window object property instead (equivalent to writing it as window.myname = 'dimitar').

    let us review your code and check it run:

    
    function fecha() {
        fecha = document.getElementById('C7');
        if (fecha.value == "") {
            alert('You must indicate the approximate date of arrival');
            return false;
        }
    }
    
    
    window.onload = function() {
        fecha();
        try {
            fecha();
        }
        catch(e) {
           alert(e.message);
        }
    };
    
    Code (javascript):
    view the demo here: http://www.jsfiddle.net/dimitar/p4Dbz/1/

    on the first run, it all works fine. the second one - not so. this is because by saying fecha = document.getElementById('C7'); in the first pass, you are effectively saying, window.fecha = document.getElementById('C7'); and that overwrites your function (that also lives at window.fecha) - the function self-destroys :)

    so a second submit will always trigger an error. ALWAYS say var when you don't need to share variables outside of their function scope. if you need to reference global variables in functions, always reference them as window.varname to save the look-up time up the scope chain and locally.

    have fun
     
    Last edited: Nov 18, 2010
    dimitar christoff, Nov 18, 2010 IP