Situation
I've got a working script that sends an ajax request to get_code.php
. This script is run from the main page - index.php
. The get_code.php
script queries my MySQL DB for a row and then sends back the data to index.php
.
Current code
jQuery in index.php
<script type="text/javascript">
$("#Code").click(function(){
var cde = $("#codeinput").val();
$.ajax({
method:'POST',
url:'get_code.php',
data: {va_code:cde},
dataType: 'json',
success: function(output_string){
$('#rtable').append(output_string);
$("#codeinput").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 * FROM variants where va_code='$code'")or die(mysql_error());
if(!$cq){
mysql_close();
echo json_encode('There was an error running the 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['cost'].'</td>';
//etc. etc. lots more output here
}
$output_string .= '</tr>';
}
mysql_close();
echo json_encode($output_string);
?>
Problem
However, if no results are found for the query, nothing is returned on the page to notify the user. Ideally I'd like to open a modal, or display a div
in which I can use the data the user input. I just can't work out for the life of me how to check if $cq
returns no results, and if so then to display something on index.php
like a notification saying 'Your code was not found'.
Appreciative of any help