Cannot process .load result correctly to change and existing set variable

Hello!

I'm trying to write some code that verifies and email address, returning Y if valid and N if not - this bit works fine, with the ValidEmail div id having the right result returned - i.e. either Y or N. However, I cannot get the valid_flaf indicator set correctly after the load event?

The alert of valid_flag always returns 0, irrespective if ValidEmail').html()  is Y or N

-------------------------------

valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }});

alert(valid_flag);

-------------------------------


Kind Regards

JamWales
JamieAsked:
Who is Participating?
 
Tom BeckConnect With a Mentor Commented:
The .load function itself and the callback run asynchronously to the other code outside of it. So, because you have the alert outside of the callback function it is executing before the callback when valid_flag still equals 9.

If you code it as follows, even though the global valid_flag variable is instantiated outside the .load function, the valid_flag value will be re-assigned inside the .load callback and you would see the correct result in the alert.

var valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }      
      alert(valid_flag);
});

This assumes of course that your Validate_EMAIL.asp file returns exactly a "Y" or an "N" only with no whitespace, other text or html. Otherwise, you may need to do something like this in your conditional:

if ($('#ValidEmail').text().indexOf("Y") >= 0) {
0
 
Tom BeckCommented:
I would say because the html for the ValidEmail div never equals "Y".

Try:

if ($('#ValidEmail').text() == "Y") {

. . .
0
 
JamieAuthor Commented:
Hi tommyBoy,

Thanks for the above.

It seems to work, in that the valid_flag now seems to be set correctly - although the alert(valid_flag); still shows 0, even when it appears to be set to 1 - but my calculation further down now works...

... however, if i remove the alert(valid_flag); statement, the valid_flag is not set correctly?!

Kind Regards

JamWales
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Tom BeckCommented:
You have code in addition to what was posted here and that is affecting the outcome. I say that because this simple test works.
<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
  $(document).ready(function(){
      var valid_flag=9;
  
      if ($('#ValidEmail').text() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }//}); 

      alert(valid_flag);
  });
</script>
</head>
<body>
    <div id="ValidEmail">Y</div>
</body>
</html>

Open in new window

0
 
JamieAuthor Commented:
Hi tommyBoy,

Yes, as you say, your script works fine - but the problem is as originally described, with the .load command. I'm not even sure what I'm trying to do is possible?

Basically, the response from the Validate_EMAIL.asp script is placed into the <div id="ValidEmail"></div> - which is then used to determine the value of valid_flag by function().

Kind Regards

JamWales
0
 
JamieAuthor Commented:
Hi tommyBoy,

I've found the cause of my problem, but not the solution!

I've amended your script below to show the issue. In my script, the variable valid_flag is set outside the .load function, so the change is not being reflected - hence why the alert(valid_flag); always returns 0 - instead of 1 for Y and 0 for N.

The below alert box will incorrectly display 9 instead of 1, as Y in ValidEmail in div tag.

Any ideas on how to resolve this?

Kind Regards

JamWales

<!DOCTYPE html>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

   var valid_flag=9;

  $(document).ready(function(){
   
 
      if ($('#ValidEmail').text() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }//});

     
  });
 
  alert(valid_flag);
 
</script>
</head>
<body>
    <div id="ValidEmail">Y</div>
</body>
0
 
Tom BeckCommented:
Any script in the head section that's not in a function is executed before the window.onload event or in this case the jquery equivalent, $(document).ready().

So...the alert is executing before the code inside the ready() function. Variable valid_flag equals 9 at that point.
0
 
JamieAuthor Commented:
Hi tommyBoy,

Thanks for your reply, I understand what you are saying, but my code below is placed into an existing function, so is not called until the function is called by the user pressing a text link on the webpage - this calls; javascript:total_cost()

From what you say and from what happens, it appears the .load function is being run local to itself and it's results not available to the function it resides it.

So not sure if it is the construct of my .load is still the problem?

Kind Regards

JamWales


valid_flag=9;

$('#ValidEmail').load('/Validate_EMAIL.asp?VerifyEmail=' + document.TestForm.AdminEmail.value,function(){

      if ($('#ValidEmail').html() == "Y") {
            valid_flag=1;
      } else {
            valid_flag=0;      }});

alert(valid_flag);
0
 
JamieAuthor Commented:
Explained the solution - and me realising what I'm trying to do in my script isn't possible.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.