shibbir ahmed shibbir ahmed - 1 year ago 120
jQuery Question

Why jQuery return data is not working as expected?

In my webpage I am using jQuery/Ajax to save form data. After save the data using php I am showing successful message like bellow :

$sql_result[] = "<div class='success'>updated</div>";

echo end($sql_result);

In Ajax return data I am checking a condition if message is contain
then I will hide a html element but It's not working.

Here is my jQuery code:

function save_email (element, event) {

e = $(element);
var formData = new FormData(e.parents('form')[0]);

data : formData,
cache : false,
contentType: false,
processData : false,

url : 'save_email.php',
type : 'POST',
xhr : function () {
var myXhr = $.ajaxSettings.xhr();
return myXhr;
success : function (data) {
if(data == "<div class='success'>updated</div>" ) {

Console log data

enter image description here


is showing this :

enter image description here

Answer Source

It's not uncommon to end up with extra whitespace in php text/html output

That is one reason it's easier to use json and check object properties , especially over comparing a complex string like you are doing.

However using trim() when comparing string results is generally a good idea


$.trim(data) === "<div class='success'>updated</div>") 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download