Jump to content

for loop...problem with what gets printed in the browser.


jimfog

Recommended Posts

I have made a for loop which examines the data object which is returned from the server with ajax.It contains a number of false/true depending on what the user submitted with the form...and after the various server-side checks have been made. there is a topic about it here:http://w3schools.invisionzone.com/index.php?showtopic=48736&hl=

 

I am not posting on the above topic cause I am dealing with a different problem. I will not get into details...what was said there, I will just show you the code and move straight on the problem description:

 success:function(data){          var fields = ['address','city','municiplaity','url'];          var missing = [];            for (var i=0; i < fields.length; i++) {                     var f = fields[i];                     if (data[f] === false) {                          missing.push(f);                             }                        }          if (missing.length > 0) {                              $('#adrserror').text('You have to fill in: ' + missing.join(', '));                        if(contains(missing, "url")) {               $('#urlerror').text('You have not entered a correct URL);                    }               }

The problem with the above code is that if URL is false it will get pushed to the missing array. this array though contains the other 3 "falses" too.These other 3 false(address,city,municiplaity...if of course the server-side checks return false) share a common attribute, They are false if the user has not completed the relevant(required fields) and as such...the message will be the same for them...meaning you have to fill in:...

 

The above though does not apply for the URL, for which I have made a separate message as you see...

The problem is URL false gets inevitable pushed to the missing array(if false) and the result is that the message that is displayed is also "You have to fill in:URL", something I do not want cause URL is not a required field.

 

 

Link to comment
Share on other sites

If you're going to just join the array like that then you can't put the URL field in that array, you need to handle it separately. Or, you can remove it from the array before you join it. Whatever your solution is, if you're just going to join that array then it's going to show every element in the array. If you don't want that to happen then don't put that field in the array.

Link to comment
Share on other sites

If you're going to just join the array like that then you can't put the URL field in that array, you need to handle it separately. Or, you can remove it from the array before you join it. Whatever your solution is, if you're just going to join that array then it's going to show every element in the array. If you don't want that to happen then don't put that field in the array.

I found a solution after all.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...