?
Solved

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

Posted on 2013-02-02
9
Medium Priority
?
225 Views
Last Modified: 2013-02-03
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
0
Comment
Question by:Jamie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38846673
I would say because the html for the ValidEmail div never equals "Y".

Try:

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

. . .
0
 

Author Comment

by:Jamie
ID: 38846749
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
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38846812
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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

Author Comment

by:Jamie
ID: 38847018
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
 

Author Comment

by:Jamie
ID: 38848178
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
 
LVL 38

Expert Comment

by:Tom Beck
ID: 38848708
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
 

Author Comment

by:Jamie
ID: 38849223
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
 
LVL 38

Accepted Solution

by:
Tom Beck earned 2000 total points
ID: 38849565
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
 

Author Closing Comment

by:Jamie
ID: 38850067
Explained the solution - and me realising what I'm trying to do in my script isn't possible.
0

Featured Post

WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question