I am using an ajax function to receive data. Based on that data, if results are not found I receive a string “No matches found” with some html formatting. If there are results, data is formatted into a table and a “Div” section underneath this section is hidden.
Now the issue I am having is, when I find no results, i do not want this “Div” table to disappear.But to check if there are no results, I would have to compare the whole string received by the searchCustomer.php (which is very large).
Is there an easier way of doing this?
My ajax call:
$('#search').click(function(e){
$.ajax({
type : 'GET',
url : 'searchCustomer.php',
dataType :'html',
data:{
lastName : $('#search_LN').val(),
firstName : $('#search_FN').val(),
phone : $('#search_PN').val()
},
success : function(data){
if (data.error == true){
alert("there was an error in the post layer");
} else {
$('#searchResults').html(data);
if (data.text == '<br><font color='red'>No matches found</font>'){
}else{
var ele = document.getElementById("NewCustomerDiv");
ele.style.display = "none";
}
}
}
});
return false;
});
Running fire-bug,
The actual data received from searchCustomer.php is:
<style type="text/css">
a.resultBookButton {
color: black;
padding: 1px;
border: 2px outset lightgrey;
background: #008800;
/* Mozilla: */
background: -moz-linear-gradient(top, lightgrey, #FFFFFF);
/* Chrome, Safari:*/
background: -webkit-gradient(linear,
left top, left bottom, from(lightgrey), to(#FFFFFF));
text-decoration: none;
}
a:active {
border-style: inset;
}
</style>
<br><font color='red'>No matches found</font>
I just want to check for “No matches found” or if there is a better way of finding no results.
I disagree with Brad, converting to JSON would be a pain because all your HTML which uses quotes all the time would have to have those quotes escaped in order to work in a JSON string, which can present an annoyance at best and accidentally trigger errors for invalid JSON at worst.
The ideal solution is changing what those pages return. Return the full HTML if items found, return “0” or “” if nothing is found, and then check for those returns.
If you can’t change what is returned from the AJAX call, perhaps just do an (returnedData.indexOf(”
Easy peasy.
EDIT: