1

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

3
  • 1
    that normally happens when there's an error on your server side script, which then in return returns an invalid json Commented Feb 19, 2018 at 11:08
  • But the query is working fine on mysql @MasivuyeCokile Commented Feb 19, 2018 at 11:11
  • You are having an invalid argument supplied for foreach.... You never fetched the results, instead you ran a loop on a boolean Commented Feb 19, 2018 at 11:14

1 Answer 1

1

I believe that you might be having an invalid argument supplied for foreach... You just executed the query and never fetched the results, then instead you ran foreach on a boolean.

after:

$execSearchLab->execute();

you need to fetch the results:

$results = $execSearchLab->fetchAll();


    $i = 0;
    foreach ($results as $result) {
        $res[$i] = $result;
        $i++;
    }
Sign up to request clarification or add additional context in comments.

5 Comments

It worked but now I got the following error at the console: searchLab.js:23 Uncaught ReferenceError: EditBtn is not defined
which line is 23?
</select><td>"+EditBtn+"</td><td>"+generateReport+"</td></tr>"
not sure why is giving that error, can see the var defined in the success
And I removed the patient.* from my query

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.