php - What is a cleaner / better way of parsing data received by an ajax success function? -


i using ajax function receive data. based on data, if results not found receive string "no matches found" html formatting. if there results, data formatted table , "div" section underneath section hidden.

now issue having is, when find no results, not want "div" table disappear.but check if there no results, have compare whole string received searchcustomer.php (which large).

is there 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 error in 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, actual data received 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 want check "no matches found" or if there better way of finding no results.

i disagree brad, converting json pain because html uses quotes time have have quotes escaped in order work in json string, can present annoyance @ best , accidentally trigger errors invalid json @ worst.

the ideal solution changing pages return. return full html if items found, return "0" or "" if nothing found, , check returns.

if can't change returned ajax call, perhaps (returneddata.indexof("

easy peasy.

edit:

$.ajax({       ... other setting ...         success : function(data){            (data.indexof('<table') == -1){                 //there table in returned data            } else {                 // ...            }         }       }); 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -