Reload div tag on select, fails at success

Hello,

trying to reload a div tag but for some reason i cant get through the if response == success statement.
According to consolLog success is returned but it keeps buy passes the if response == success and i cant seem to pin pint why the if statement isnt evaluated. any ideas?

$(document).ready(function() { /// Wait till page is loaded
     $('#idSel_project').change(function(){                  
          var prjid = $("#idSel_project").val();       
          if (prjid == '') { 
               $('.success').fadeOut(200);
               $('.error').fadeIn(200);               
          } //END of checking prjid variable
          else {
               $.ajax(
                    { type: "POST",                   
                       url: 'phpFile.php', 
                      data: $('#testForm').serialize(),
                      success: function(response){
                         **console.log("Stage 1 resp:: "+ response);**                        
                         if(response == 'success') {                              
                              $('.success').fadeIn(200).show();
                              $('.error').fadeOut(200).hide();
                              **console.log("Stage 2: "+ response);**                             
                              $("#prjid").val("");                            
                         } // end of if statement
                         else { 
                              $('.success').fadeOut(200);
                              $('.error').fadeIn(200);                              
                         } //end of else statement
                      },//end of success function
                      error: function() { 
                         $('.success').fadeOut(200);
                         $('.error').fadeIn(200);                        
                    }// end of error function
               });//end of ajax statement and begining of type: post 
          }//END of else statement if it pass the field empty check
          return false;
     }); //End of change function
}); // End of ready function - Wait till page is loaded

Just think… Could you try response === ‘success’ with three ‘=’, not with two?

Hi igor_g,
I figured out why but not sure what caused it. when the the results from the PHP (simple php file that echo either success or failed message) was returned something appended to the returning values so every time the if statement evaluated “success” it would fail because there a strange character embedded in the return value. so with nothing to loss i tried $.trim() and it fixed the problem. only spent 2 days… not bad…lol

when the the results from the PHP (simple php file that echo either success or failed message) was returned something appended to the returning values so every time the if statement evaluated “success” it would fail because there a strange character embedded in the return value.

That could be, because you close php tags

<?php

//your code

?>

should be

<?php

//your code

1 Like

Closing tags on their own shouldn’t be an issue, surely? Having white space after the closing tags might, though. Changing

**console.log("Stage 1 resp:: "+ response);**                        

to read

**console.log("Stage 1 resp:: ~"+ response + "~");**                        

might show what the issue was / is.

Closing tags on their own shouldn’t be an issue, surely?

As I know, not closed tag in PHP-script is standard, exactly to avoid empty charachters in response.

Fair enough, I have little experience of this. But as I am learning PHP, by habit I always close the PHP tags. I did wonder for a moment whether that’s the reason that I sometimes have a problem with a response from an Ajax look-up, but it turns out that I’m returning the data as XML in that case, rather than just plain text, so the extra whitespace would be after the last tag.

Ok, so curiosity hit me. Did a quick test with what droopsnoot posted and sure enough there is a blank space at the end of the results. I’m not sure where its caming from but its there.
good troubleshooting tip, droopsnote… thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.