AJAX Response When Query Returns Empty Data

Hello Experts!

I normally populate DataTable with the following combination. When a button is clicked, the table is populated through AJAX call as long as there's at least 1 row of data. But if there're no data in the table from MySQL, nothing happens. So, I want a message to return when there's no data coming from database.

Please help me manipulate this:

<?php 

require_once '../.../db_connection.php';

$stmt = $conn->prepare( "SELECT ... FROM ... WHERE ..." );
$stmt->bind_param( "ssssi", $_POST['session'], $_POST['term'], $_POST['class'], $_POST['subject'], $_POST['limit'] );
$stmt->execute();

$assessment = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
die( json_encode($assessment) );
?>

Open in new window



Here is my AJAX:
$('#loadStudents').click(function() {
   $.ajax({
   method : 'post',
   url : '../data/ass1_Data',
   data : { session : $('#session').val(), term : $('#term').val(), class : $('#c_taught').val(), subject : $('#s_taught').val(), limit : $('#process_limit').val(), },
   dataType : 'json',
   }).done(function(data) {
      myDt.clear().draw();
      myDt.rows.add(data).draw();
   });
});

Open in new window


Thank you in advance.
LVL 1
Opeyemi AbdulRasheedAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
in your callback

.done(function(data) {
  If (data.length) {
    // there were records so process them
    myDt.clear().draw();
    myDt.rows.add(data).draw();
  }
  else {
    // do your "no records found here"
  }
});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Zakaria AcharkiAnalyst DeveloperCommented:
Try to add the emptyTable option to the definition of your datatable object :

var myDt = $('#my-datatable').DataTable( {
    "language": {
        "emptyTable":     "There no row message."
    }
} );

Open in new window

Opeyemi AbdulRasheedAuthor Commented:
@Julian Hansen, thank you sir, but there's syntax error. The IF under .done(function(data) { shows a wrong color code in my Sublime Text, which means syntax error. Kindly check, sir.

@Zakaria Acharki, thank you for your help, sir. There's already a "No data available in table" as built-in message for emptyTable. I just want an action response after clicking on Load Button.
Opeyemi AbdulRasheedAuthor Commented:
@@Julian Hansen, forgive me sir. I copied pasted your code (CAPITAL If). Lazy me. I figured it out after sending the previous comment. It's working perfectly, sir.
Opeyemi AbdulRasheedAuthor Commented:
Thanks both
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.