If statement condition not working

hello,

I have a script that reloads a form without refreshing the page. Everything seems to be work fine except when the java script hits the if statement, for some reason. The if statement if(msg == ‘Saved’) conditions seems to never met.
as you can see from the code below i set a couple of alerts and at stage2 alert and at stage 4 alert everything works. event the the variable returned (msg) is confirmed to have the correct value.

any ideas as to whats causing the if statement to by pass?


		{	alert('stage2');
			$.ajax(
			{	type: 'POST',
				url: "emailposter.php",
				data: $('#emailForm1').serialize(),
				success: function (msg)
				{	if(msg == 'Saved')
					{	$('.emailSuccess').fadeIn(200).show();
						$('.emailError').fadeOut(200).hide();
						$("#txt_email").val("");	
						alert('stage3(worked)' + '  ' + msg);						
					}
					alert('stage4(content of msg)-->: '+' ' + msg);
				},
				error: function ()
				{	$('.emailSuccess').fadeOut(200);
					$('.emailError').fadeIn(200);
					alert('stage5');					
				}
			});
		}



<?php
$test = $_POST['txt_email'];
if($test)
{   echo 'Saved'; }
else
{   echo 'Not Saved'; }

Hi,

Can you change your success callback to the following and post what is logged:

success: function (msg){
  console.log(msg);
},

This article explains how to access the console if you are not sure.

hello pullo
i copy / pasted the line of code and nothing happend…
my js file is a external file and not sure if that has anything to do wtih you line of code.

You’re going to need to find out what’s being returned somehow.

AJAX returns an object so it will never equal any string value.

thanks for the reply back… after pullo post i looked at the tutorial in the post and i have screen shot maybe you can look at to help me with this issue…
next to the saved theres a return key… is that whats causing the problem…
see screen shot

Try

success: function (returned_data){
  console.log(returned_data['msg']);
},

ok, still not resolved by getting close.
i think it has somehting to do with this line data: $(‘#emailForm1’).serialize(),
now, how do i handle serialize… i think the return value is a object and when i compare object and ‘saved’ its not working.
any ideas how to fix this?

The ‘msg’ is “Not Saved”?

hi Mittineague
originally i had another if statement and if it met “not saved” do something… but i took that out from the javascript to make the debugging easy…

Hi,

This is actually not true in the case of jQuery. Using the $.ajax() method, you can set the dataType attribute to specify the type of data that you’re expecting back from the server. If none is specified (as here), jQuery will try to infer it based on the MIME type of the response and it is quite conceivable that it will default to a plain text string.

As for the OP’s original problem, something else is at play. I just made a simple demo:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>AJAX test</title>
  </head>
  <body>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
      $.ajax({ 
        type: "POST",
        url: "submit.php",
        data: {"test": "test"},
        success: function (msg){ 
          if(msg === "Saved"){
            console.log("The server saved my data!")
          }
        }
      });
    </script>
  </body>
</html>
<?php 
echo "Saved";

And, as expected, the message “The server saved my data!”, was logged to the console.

@robin01 ;
Could you post a link to a page where we can see the problem or enough code that we can reproduce it ourselves?

Thanks for correcting.

I’ve used jQuery for JSON (with header('Content-type: application/json');) and missed that it can do other.

A good thing to be aware of for future projects.