Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

442
Views
PHP AJAX Notifications if MySQL query returns no rows

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">
$("#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 * 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['cmtcost'].'</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

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could return a result flag. This is a good logic and easy to understand.

If no row was found :

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

If some 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 it helps.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!