I am having this PHP script where I want to search the database for a specific text:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once('../php/connection.php');
header('Content-type: application/json');
$res = array();
$cid = $_SESSION['clinic_id'];
$searchTxt = '%'.$_POST['searchTxt'].'%';
try{
$searchLab = "SELECT
CONVERT(aes_decrypt(patient.patient_name_en, '2017') USING utf8mb4) as 'pn',
lab_test.patient_id,
lab_test.lab_id,
lab_test.lab_status,
lab_test.test_date,
patient.*
FROM patient
LEFT JOIN
lab_test
ON
patient.patient_id = lab_test.patient_id
WHERE
lab_test.clinic_id = :cid
AND
(lab_test.patient_id LIKE :searchTxt
OR
aes_decrypt(patient.patient_name_en, '2017') LIKE :searchTxt
OR
lab_test.test_date LIKE :searchTxt)
ORDER BY lab_test.test_date";
$execSearchLab = $conn->prepare($searchLab);
$execSearchLab->bindValue(':cid', $cid);
$execSearchLab->bindValue(':searchTxt', $searchTxt);
$execSearchLab->execute();
//$execSearchPatientResult = $execSearchPatient->fetchAll();
$i = 0;
foreach($execSearchLab as $result)
{
$res[$i] = $result;
$i++;
}
echo json_encode($res);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
?>
I tested out the query in wampserver and it is working fine.
Now in the Javascript part, I am getting the returned JSON array and display it in an html table:
var searchFunction = function(){
var searchTxt = $("#searchTxt").val();
searchTxt = $.trim(searchTxt);
//console.log(searchTxt);
$.ajax({
url: '../php/searchLab.php',
type: 'POST',
data: {searchTxt: searchTxt},
dataType: 'JSON',
success:function(resp)
{
//append data
$("#patient_table tr").fadeOut(400);
$("#after_tr").before("<tr class='bg-info'><th>ID</th><th>Patient ID</th><th>Name</th><th>Date of test</th><th>Status</th><th>Change Status</th><th colspan='5' style='text-align:center'>Actions</th></tr>");
$.each( resp, function(key, result)
{
//console.log(JSON.stringify(result));
var pid = result['patient_id'];
var editBtn = "<a id='editBtn'><span class='badge badge badge-info' style='background-color: #0090ff'>Edit</span></a>";
var generateReport = "<a id='generateReport'><span class='badge badge badge-info' style='background-color: #0090ff'>Generate Report</span></a>";
$("#after_tr").after("<tr id="+result['lab_id']+"><td>"+result['patient_id']+"</td><td>"+result['patient_name_en']+"</td><td>"
+result['test_date']+"</td><td>"+result['status']+"</td><td><select style='color: #0090ff; ' class='form-control select patient_status' name='lab_status'><option value='select'>Select</option><option value='Active'>Active</option><option value='Inactive'>Inactive</option></select><td>"+EditBtn+"</td><td>"+generateReport+"</td></tr>");
error: function (jqXHR, textStatus, errorThrown) {
alert('Not done - ' + textStatus + ' ' + errorThrown);
}
});
}
$(document).ready(function()
{
$("#searchTxt").on('keyup', searchFunction);
$("#searchBtn").on('click', searchFunction);
//$("#searchTxt").on('change', searchFunction);
});
I can't see any data in my page and I have the following error:
Not done - parsererror SyntaxError: Unexpected end of JSON input