$postVars = array('question', 'answer'); // Form fields names //Validate the POST variables and add up to error message if empty foreach ($postVars as $postVar) { switch ($postVar) { default: $faqObj->{$postVar} = filter_input(INPUT_POST, $postVar) ? mysqli_real_escape_string($dbObj->connection, filter_input(INPUT_POST, $postVar)) : ''; if ($faqObj->{$postVar} === "") { array_push($errorArr, "Please enter {$postVar} "); } break; } } //If validated and not empty submit it to database if (count($errorArr) < 1) { echo $faqObj->add(); } else { $json = array("status" => 0, "msg" => $errorArr); $dbObj->close(); //Close Database Connection header('Content-type: application/json'); echo json_encode($json); } } if (filter_input(INPUT_POST, "fetchFaqs") != NULL) { $requestData = $_REQUEST; $columns = array(0 => 'id', 1 => 'question', 2 => 'answer', 3 => 'date_added'); // getting total number records without any search $query = $dbObj->query("SELECT * FROM faq "); $totalData = mysqli_num_rows($query); $totalFiltered = $totalData;