• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Variable not changing before form submit?

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all. I am using a button that calls a method to validate. If all validations pass, then I call the form.submit() to send the information to the server.

so for example, the button calls validate which is something like this:



For some reason, the variable submit doesn't seem to change. It's default value is "true", and if anything is validated as false, then the submit variable is "supposed" to change from true to false in the for loop. It does not however, and still submits even when it should not.

Here's the weird part. If I put an "alert" in between the for loop and the if statement, it works fine!!! It's almost as if the alert gives the javascript time to actually change the variable value before it gets to the if statement. I'm really new to javascript and very frustrated right now. Any help would be greatly appreciated.

Thanks!

-Nate

[ May 30, 2006: Message edited by: Nate Leech ]
[ May 30, 2006: Message edited by: Bear Bibeault ]
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I personally would not use submit as a variable name. Would use bSubmit.

Also store in true and not "true", you are talking about a boolean vs. string here.

then do



Eric
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Eric Pascarello:
I personally would not use submit as a variable name. Would use bSubmit.



I will respectfully disagree with Eric on this one. Hungarian notation (preceeding the variable name with its type) is a pretty archaic practice and just makes your code less readable.

"submit" is not a Javascript reserved word so that's not likely the problem.

I heartily agree with Eric that the variable should be declared local to the function, and you should use the boolean literals and not strings.
[ May 30, 2006: Message edited by: Bear Bibeault ]
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear, I was thinking submit was a reserved word since JavaScript has a bunch. I looked at my list and realized it wasn't. I personally would be using is, but that is a different story.

Eric
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually I agree, in such a scenario I would use something like 'ok' which reads better to me:

 
Nate Leech
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the help!

Out of curiosity, do you know what exactly the problem was with the way I was doing it?
Was it something specific with Javascript that made the validation work with an alert between the for and the if but not work without it?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nope, doesn't ring a bell. Is it working for you now?
 
Nate Leech
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite yet, only because I can't figure out how to get my validate method to return a value easily.

I'm actually using the Ajax methods from Eric's book for validation. Here is basically what I'm doing for the validation part:

On the .jsp I have a dynamic form that lets the user add text fields so they can add users to their object. (Similar to how gmail adds attachments) So these fields can vary in number. So I have a servlet that takes a username and verifies their certification and verifies if they even exist at our institution. If anyone in the list fails this validation, I display an error message on the page and let the user know which users don't exist and which users still require certification in order to be added to the object they wish to create.

So the validation is like this:



So from Eric's book, what the net.ContentLoader is doing is passing the url in and running the servlet to get the xml back. Then the "certify" method is called. My certify method pretty much just prints out an error message to the page.
What I'd like to do is have the certify method return a boolean value as well to tell whether or not validation passed, that way I can use the local bSubmit variable that was suggested. But because the "certify" method is called from within the "ajaxValidateUser" from the net.ContentLoader constructor as a parameter instead of just calling it directly, I'm not sure how to cascade the return value up from the "certify" method to the "ajaxValidateUser" method to the "validate" method.

This is why I was originaly using the global variable "submit". I would just set it to false if validation failed within "certify".

I hope this makes sense. Any more help would be great. Lemme know if i should clarify anything. Thanks again for the help so far.

-Nate
[ May 31, 2006: Message edited by: Nate Leech ]
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I understand, you looking at this from the wrong point of view.
You trying to solve it using traditional programming, but the keyword in AJAX is "Asynchronous", meaning you should use event driven philosophy.

In your particular example "certify" will be called after ajaxValidateUser is done. So the way you implementing it, you would have to go with something like:
var pointer - current loop pointer
function1 - increases pointer, gets next UserName, sends request to check;
function2 - called when server replayed by AJAX implementation. Should handle correct/incorrect user names and then calls function1

Handling of the moment when you reached the end of the array can be put into any of those functions.

Aside from that, in your particular case may be much more efficient to actually send a post request with all user names to be validated at once, that do do it one by one.

Also, keep in mind that while AJAX is a very popular and convinient thing, there are other means to have the same effect. Years before the AJAX declared itself people used hidden IFRAMES and SCRIPT tag - and it may fit better in your design.
 
Nate Leech
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yuyiy,

Thanks for the advice. I'll give your suggestions a try to see if I can get this to work.

Thanks!

-Nate
 
Nate Leech
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Got it working, thanks for the help, everyone!!!

-Nate
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic