Link to home
Start Free TrialLog in
Avatar of Charlietn
Charlietn

asked on

Why won't this work?

Probably very silly but I cant make this work,   I am doing an ajax query to the database and if the criteria, in this case last name, come back with multiple rows my responsetext is there are multiple clients.    It is being returned.  I just want to do a simple evaluation if it comes back with 1 name process it normally if there are multiples I have to populate a pick list.  

the attached code is what has me frustrated  The string is returned correctly each time.

Thanks
function handleStateChange() {                                 
   if (xmlHttp.readyState == 4) {                              
      if (xmlHttp.status == 200) {    
   var retSTR =" ";
   var retSTR = xmlHttp.responseText;
	  if (retSTR === "There are multiple Clients"){
        alert(retSTR);  }
		 }else{	                      	   	
	 parseResults(); }
      }                                                        
   }                                                                                                               
}

Open in new window

Avatar of HonorGod
HonorGod
Flag of United States of America image

> I am doing an ajax query to the database ...

  So, you are actually having your Ajax request sent to a server side application that is actually executing the DB query... right?

  Is the callback routine being called?
function handleStateChange() {                                 
   if (xmlHttp.readyState == 4) {                              
      if (xmlHttp.status == 200) {
          var retSTR = xmlHttp.responseText;
          alert( 'status 200: "' + retSTR + "' )
	  if ( retSTR === "There are multiple Clients") {
              alert(retSTR);  }
          } else {	                      	   	
              parseResults();
          }
      }                                                        
   }                                                                                                               
}

Open in new window

Avatar of Charlietn
Charlietn

ASKER

Yes,  the server returns the string and all works well.   parseResults() handles the split and fills in the fields.    If the query returns more than 1 row I return the morethanone string.  If it comes back as "There are multiple Clients" I need to allow the user to select from a list.  If it does not come back I need parseResults to execute

THanks.
So, you need to decide how to respond to the "There are multiple Clients" returned result?

I would consider having a portion of the page used to display the "multiple results", and allow the user to select the one(s) to be processed.

What, exactly, are you looking for with this question?
I cant get the if statement to work.
if ( retSTR === "There are multiple Clients") {
              alert(retSTR);  }
          } else {	                      	   	
              parseResults();

Open in new window

Are you seeing an alert dialog box generated by this statement?

If so, please add a copy of the dialog box here for our review.

Thanks
alert( 'status 200: "' + retSTR + "' )

Open in new window

No Sir.   I just need to evaluate the returned string and act on it  That is what is failing.  the alert (retStr) is there only for me.  It will be relaced by another function.  

When my string is returned from the server it DOES contain "There are multiple clients"  but it does not trigger the alert.  When I get the If statement to work if the returned string contains "There are multiple clients"  I want to run a function.  If it does not contain "There are multiple clients" I want parseResults to run.  

if string contains ""There are multiple clients"
  do this
if it does not contain "There are multiple clients"
 do that

right now when the return string contains "There are multiple clients" the alert does not fire.  I assume there is something wrong with my if statement.

Thanks

 
function handleStateChange() {                                 
   if (xmlHttp.readyState == 4) {                              
      if (xmlHttp.status == 200) {
          var retSTR = xmlHttp.responseText;
	  if ( retSTR === "There are multiple Clients") {
              alert(retSTR);  }
          } else {	                      	   	
              parseResults();
          }
      }                                                        
   }                                                                                                               
}

Open in new window

function handleStateChange() {                                 
   if (xmlHttp.readyState == 4) {                              
      if (xmlHttp.status == 200) {
          var retSTR = xmlHttp.responseText;
	  if ( retSTR === "There are multiple Clients") {
              alert(retSTR);  }
          } else {	                      	   	
              parseResults();
          }
      }                                                        
   }                                                                                                               
}

Open in new window

function handleStateChange() {                                 
   if (xmlHttp.readyState == 4) {                              
      if (xmlHttp.status == 200) {
          var retSTR = xmlHttp.responseText;
	  if ( retSTR === "There are multiple Clients") {
              alert(retSTR);  }
          } else {	                      	   	
              parseResults();
          }
      }                                                        
   }                                                                                                               
}

Open in new window

If the alert does not fire, then the string you are expected is not what is present.

That is why I suggested that you add the alert() in line 5 here: https://www.experts-exchange.com/questions/27397345/Why-won't-this-work.html?anchorAnswerId=36969953#a36969953

This alert should be generated when the status is 200 (i.e., "OK").

If you aren't seeing this alert dialog box, then we should add an alert showing:

- handleStateChange() is being invoked
- xmlHttp.readyState has a value of 4

Does this make sense?
The string is present.  If I just put the alert in without the if statement it fires and shows "There are multiple clients"   That is why I am questioning the if statement.

Thanks
function handleStateChange() {                                 
   if (xmlHttp.readyState == 4) {                              
      if (xmlHttp.status == 200) {
          var retSTR = xmlHttp.responseText;
	 alert(retSTR);  }
               }                                                        
   }                                                                                                               
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of HonorGod
HonorGod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK.  Thanks.  I will try it and let you know

Charlie
returnedString.match

Worked

Thanks
Super.  I'm glad to be able to help.

Good luck & have a great day.