Results 1 to 8 of 8

Thread: Won't return form to function

  1. #1
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default Won't return form to function

    Hey,

    Well I have an issue where it's not returning to the function like it should, I'm not sure why not but it wont even alert. See if you can find my error, cause I sure cant. Bah.

    Form:
    <div id="popup">
    <form name="login" method="post" action="" onsubmit="return login();">
    <p>
    <label>Username</label><br>
    <input type="text" name="user" class="txt"><br>
    <label>Password</label><br>
    <input type="password" name="pass" class="txt"><br>
    <input type="reset" value="Close" class="btn" onclick="popUp();">
    <input type="submit" value="Login" class="btn">
    </p>
    </form>
    </div>
    Script:
    var o;

    function login() {
    window.o = document.getElementById('popup');
    if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
    }

    else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4) {window.o.innerHTML=xmlhttp.responseText;} };

    window.o.innerHTML = 'Attempting to log in...';
    xmlhttp.open('GET','../scripts/login.php',true);
    xmlhttp.send(null);
    return false;
    }
    Arrrrgh. Stupid mistakes, they irritate the crap outta ya.

    Thanks!

  2. #2
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    Come on I know SOMEBODY knows

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Apparently login the function is being overwritten by login the form. Change the name of one or the other of them, like:

    Code:
    <form name="logger" method="post" action="" onsubmit="return login();">
    Also, this will not work locally in IE 7+ due to security restrictions, but should be fine in those browsers live.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    twQ (01-18-2010)

  5. #4
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    Well that's odd. Why would a forms name overwrite a JavaScript function? Huh? I didn't know that happened. I guess I missed that one in class haha.

    Thanks John!

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Well just about anything can overwrite just about anything. For it to happen both things have to be available to whatever the current scope is.

    I don't think I've seen this exact thing happen before. It's probably a partial holdover from earlier days where simply the name of a form or image represented that form or image as an object. I think this still applies to images, at least in some browsers. Here it's something different though because if you do an alert after the form, it alerts the function.

    Code:
    alert(login);
    But if you do it from within the submit event attribute, it alerts the form:

    Code:
    onsubmit="alert(login);return false;"
    Behaves the same in Firefox and IE, pretty strange.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #6
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    That is very odd. I probably would've never thought to try that and just scrapped the script haha. Thanks a bunch.

    New problem though. The new code won't stop the form in Opera, I'm sure it's something technical but I figure this threads open you're already looking at it so I might as well save myself a tiny bit of trouble and get a little help on this matter.

    Code:
    var p = 0;
    var o;
    var c;
    
    function popUp() {
    window.o = document.getElementById('popup');
    if (!window.p) {
    window.o.style.display = 'block';
    window.p = 1;
    document.forms.log.user.focus();
    }
    
    else {
    window.o.style.display = 'none';
    window.p = 0;
    }
    }
    
    function login(f) {
    if (!f) {
    window.o = document.getElementById('popup');
    if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
    }
    
    else {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4) {window.o.innerHTML=xmlhttp.responseText; return false;} };
    
    window.c = window.o.innerHTML;
    window.o.innerHTML = 'Attempting to log in...';
    xmlhttp.open('GET','scripts/login.php',true);
    xmlhttp.send(null);
    }
    
    else {
    window.o.innerHTML = window.c;
    }
    }
    It just goes ahead and submits.

    Thanks,
    Tim

  8. #7
    Join Date
    Dec 2009
    Location
    North Carolina
    Posts
    71
    Thanks
    13
    Thanked 3 Times in 3 Posts

    Default

    To help whoever decides to help out I am pretty sure it is related to the way I implement my return false, but if I put it earlier the script doesn't return it's work so...hmmmm?

    The script will, if not done with Ajax, submit like a normal form, but I still intend to fix Opera to make Ajax function properly.


    Thanks DD goers - especially John,
    Tim

  9. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Your function has no return value. Some browsers will override the default action even with no return value. Technically this is incorrect. So Opera is actually doing it 'right' here. Just add a return false:

    Code:
     . . . nerHTML = 'Attempting to log in...';
    xmlhttp.open('GET','xmlfile.txt',true);
    xmlhttp.send(null);
    }
    
    else {
    window.o.innerHTML = window.c;
    }
    return false;
    }
    And be sure to add the event like so:

    Code:
    <form name="logger" method="post" action="" onsubmit="return login();">
    Alternatively, you may add the return value to the event attribute:

    Code:
    <form name="logger" method="post" action="" onsubmit="login(); return false;">
    Note: Your onreadystatechange function does have a return value of false when successful. However, the onreadystatechange function is not the onsubmit event of the form and has no influence over the return value of the login function.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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