aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Variable not changing before form submit? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Variable not changing before form submit?" Watch "Variable not changing before form submit?" New topic
Author

Variable not changing before form submit?

Nate Leech
Ranch Hand

Joined: Feb 07, 2005
Posts: 43
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 ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Actually I agree, in such a scenario I would use something like 'ok' which reads better to me:

Nate Leech
Ranch Hand

Joined: Feb 07, 2005
Posts: 43
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61420
    
  67

Nope, doesn't ring a bell. Is it working for you now?
Nate Leech
Ranch Hand

Joined: Feb 07, 2005
Posts: 43
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 ]
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
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

Joined: Feb 07, 2005
Posts: 43
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

Joined: Feb 07, 2005
Posts: 43
Got it working, thanks for the help, everyone!!!

-Nate
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variable not changing before form submit?