Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Ajax and Forms

  1. #11
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default

    This works, but a bit hackish.... I first set all the vars to true and then they get changed to false as the user fills out the form. I tried the array thing, but couldn't get it to work out.

    Code:
    function checkForm(form){
            if (usernameerror || pworderror || emailerror || firstnameerror || lastnameerror){
              document.getElementById('form_help').innerHTML = '<img src="images/info.jpg" alt="" width="25" height="24" />&nbsp;&nbsp;Please correct any errors and re-submit.';
            return false;
             } else {
              return true;
             }
    }

  2. #12
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Lemme guess, the site you just redid was tables and you're converting them to CSS?
    LOL! That and AJAXIFY it.
    Damn. I should have charged more!
    Well, we cant always expect our clients to pay for our education. You'll have good tools after this one.
    This works, but a bit hackish....
    Yeah.... not a good idea....
    The problem i'm having with using one 'anyerror' var is that when an element is correct, it changes the anyerror back to true.
    Hmmm..... are you sure????
    anyerrors=(!ok ? true : anyerrors);
    What this should do is, set anyerrors to false first then, if we get a 'false' (!ok) from the validation, we set anyerrors to true.
    If the validation returns tru, then we just do a dummy operation of making anyerrors equal anyerrors.
    Problem is, if your validation doesn't return anything, I think THAT is the problem. Try this instead
    anyerrors=(ok==false ? true : anyerrors);
    This way if you slipped and did not return an explicit true, which I will bet is the issue here, it will explicitly look for a false.
    !ok is false, OR no value.

  3. #13
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default

    Jmarc,

    I think I did what you mention above, just a bit backhanded way of getting there. First, I set all error vars to "true" that way, each validation function has to explicitly make them true or leave them false:

    Code:
     var usernameerror=true;
        var pworderror=true;
        var emailerror=true;
        var firstnameerror=true;
        var lastnameerror=true;
    By the way, I found out the hard way about variable declaration as it relates to scope in JavaScript. Weird thing is that if you declare a variable with the word 'var' inside a function, it is private. To make it global, drop the var. Strange how that works. Outside a function, variables are global. How 'bout that? Strange and bewildering.

    Anyway, I'm still trying to wrap my head around your way. Give me a little more time with it... and thanks again!

  4. #14
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Backhanded indeed. Not a problem if it works. But not a good idea. 10 or 20 years later (I have software from 1990s still running, so dont kid yourself!) you'll be scratching your, uhhh.... head wondering what in hell you were doing. Oh, and this is where we repeat the DOCUMENTATION mantra....
    So, blitzkrieg programming? Fine, but at some point, in your leisure time, try to get it right. True is true, it is not false even if you CAN use it that way. Matt can tell you just how pleased I was when I inherited some perl code that was.... well.... with all the cursing I did over the guys code, I am sure there was an immediate karmic wave that capsized his boat somewhere.....

    Yeah, var. Wouldnt it be nice if these bums would tell you what was up with that before giving examples?

    However, var is a lifesaver. How clean is YOUR naming conventions? Nice to name it what you want in a function and NOT have to worry at all with what is going on with naming anywhere else.

    Keep the spirit. Learning the easy way sucks. Feels nice, but...

  5. #15
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default

    Now you're making me feel guilty. OK,OK. Here's what I'm going to do -- put it on the list of clean up items for the site - it's working now, but need to come back to it. I'm back to php for the moment and trying to trouble shoot a SQL error for the lost PW script. I am a big believer in doing things right, so I want to get that right as well, just have to digest it a little further.

    As far as the "shortcut" in coding for this:

    Code:
     anyerrors=(!ok ? true : anyerrors);
    is that the same as?:

    Code:
    if (anyerrors !=OK){anyerrors=true;}
    Btw, John-Marc, do you work for yourself right now? Just curious...

  6. #16
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Be guilty! Be VERY guilty! LOL!
    As for the code, no its not the same.
    Your code doesnt tell us if there were any errors.
    Only if the 2 are different. So ultimately, it gives us little useful info.
    First, in the spirit of doing it right we should really be doing
    Code:
    anyerrors=(ok==false ? true : anyerrors);
    because ok could be null, which would trigger it.
    That code is a one line version of an if/else, but in this version, the 'else' is required.
    Long version:
    Code:
    if (ok==false){
     anyerrors=true;
    } else {
     // Do nothing
    }
    Since anyerrors is a flag to tell us if there were EVER any errors in ANY of the validations, we set it false, and we loop through ALL validation to see if any come back false which is when we set it true to indicate there was an error. By maintaining your functions to return a proper true or false, you can continue to use them individually when a user changes a single value before submitting.
    You could also do something like this:
    Code:
    anyerrors=0;
    ok=...
    if (ok==false)
     anyerrors++;
    ok=...
    if (ok==false)
     anyerrors++;
    alert('# of errors: '+anyerrors)
    I opted for a simple "is there or is there not any errors" because your validation seems to use dhtml nicely to indicate right on the field itself that there is one or more error.
    And, yes, I am a solo act.

  7. #17
    jmarcv's Avatar
    jmarcv is offline Cranky Coder
    Join Date
    Jan 2005
    Posts
    354

    Default

    Yo, CH!

    Curious to see how things went, or did you shelve it?

  8. #18
    charlesh's Avatar
    charlesh is offline Master Glow Jedi
    Join Date
    Aug 2006
    Location
    Atlanta, GA - better than you imagined it would be.
    Posts
    189

    Default

    Shelved it for now - had been developing the admin console right now, but plan to go back to update the form (the good way

    Thanks for the help!

    Charles

Page 2 of 2 FirstFirst 12

LinkBacks (?)


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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16