jquery - PHP AJAX Notifications if MySQL query returns no rows -


situation

i've got working script sends ajax request get_code.php. script run main page - index.php. get_code.php script queries mysql db row , sends data index.php.

current code

jquery in index.php

<script type="text/javascript"> $("#cmtcode").click(function(){     var cde = $("#cmtcodeinput").val();         $.ajax({             method:'post',             url:'get_code.php',             data: {va_code:cde},             datatype: 'json',             success: function(output_string){                     $('#rtable').append(output_string);                     $("#cmtcodeinput").val('');                     var prc = $(".price:last");                     prc.focus();             }         }); });     </script> 

php script get_code.php

<?php include('dbc.php'); $code = $_post['va_code']; $cq = mysql_query("select * variants va_code='$code'")or die(mysql_error());   if(!$cq){ mysql_close(); echo json_encode('there error running query: ' . mysql_error());   }elseif(!mysql_num_rows($cq)){ mysql_close(); echo json_encode('no results returned');   }else{ $output_string = ''; $output_string .= '<tr>'; while($row = mysql_fetch_assoc($cq)) {              $output_string .= '<td>'.$row['cmtcost'].'</td>'; //etc. etc. lots more output here }      $output_string .= '</tr>'; } mysql_close(); echo json_encode($output_string); ?> 

problem

however, if no results found query, nothing returned on page notify user. ideally i'd open modal, or display div in can use data user input. can't work out life of me how check if $cq returns no results, , if display on index.php notification saying 'your code not found'.

appreciative of help

you return result flag. logic , easy understand.

if no row found :

die(json_encode(['result' => false, 'error' => 'no code found'])); 

if code found :

die(json_encode(['result' => true, 'output' => $output_string])); 

and in js :

... success: function(data){     if (!data.result) {         alert(data.error);     } else {         $('#rtable').append(data.output);         $("#cmtcodeinput").val('');         var prc = $(".price:last");         prc.focus();     } } ... 

hope helps.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -