-2

i used typeahead in my codeigniter 3 project and it worked well on localhost but when i ran it on live server it failed. i am unable to resolve it so help is required. My view input field is.

<div class="col-md-12"> 
<input type="text" id="refby" name="refby" style="" placeholder="ref by"   /></div>

my js code is

$("#refby").typeahead({
    source:  function (query, process) {
    return $.get("<?php echo base_url('get_doctor'); ?>", { query: query }, function (data) {
            console.log(data);
             data = $.parseJSON(data);
            return process(data);
        });
    }
});

controller function is

public function get_doctor()
{
    $check = $this->input->post('check');
    $data = $this->customers->get_doctor($check);
    echo json_encode( $data);
}

model function is

function get_doctor($check)
    {
        print_r($check);
    $this->db->select('name');
    $this->db->like('name', $check);
    $this->db->from('doctors');
    $query = $this->db->get();
    return $query->result();
    }

script link is

<script src="<?php echo base_url('assets/datatables/js/bootstrap3-typeahead.js'); ?>"></script>

console log is

<div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>A PHP Error was encountered</h4>
<p>Severity: 8192</p>
<p>Message:  mysqli::real_escape_string(): Passing null to parameter #1 ($string) of   type string is deprecated</p>
<p>Filename: mysqli/mysqli_driver.php</p>
<p>Line Number: 401</p>
<p>Backtrace:</p>   
<p style="margin-left:10px">
               File:home/vol5_2/infinityfree.com/if0_39199666/htdocs/application/models/Customers.php<br />
Line: 300<br /> Function: like</p>      
        <p style="margin-left:10px">
        File: /home/vol5_2/infinityfree.com/if0_39199666/htdocs/application/controllers/Customer.php<br />
        Line: 346<br />
        Function: get_doctor</p>        
        <p style="margin-left:10px">
        File: /home/vol5_2/infinityfree.com/if0_39199666/htdocs/index.php<br />
        Line: 315<br />
        Function: require_once</p>
</div>[{"name":"SELF"},{"name":"DR. ASIM IRSHAD"},{"name":"DR. ABDUL HAMEED KHAN"},{"name":"DR. MUHAMMAD SAFDAR CH"},{"name":"DR. SAFDAR HUSAIN BUKHARI"},{"name":"DR. ANSAR MEHBOOB"},{"name":"DR. SAJID MEHMOOD KHAN"},{"name":"DR. MAZHAR ABBAS KHAN"},{"name":"DR. ASMA MALIK"}]
VM135:2 Uncaught SyntaxError: Unexpected token '<', "
<div style"... is not valid JSON
at JSON.parse (<anonymous>)
at n.parseJSON (jquery-2.1.4.min.js:4:5497)
at Object.success (reception:626:27)
at j (jquery-2.1.4.min.js:2:26925)
at Object.fireWith [as resolveWith] (jquery-2.1.4.min.js:2:27738)
at x (jquery-2.1.4.min.js:4:11253)
at XMLHttpRequest.<anonymous> (jquery-2.1.4.min.js:4:14765)

while on localhost console log is

[{"name":"SELF"},{"name":"DR. ASIM IRSHAD"},{"name":"DR. ABDUL HAMEED KHAN"},{"name":"DR. MUHAMMAD SAFDAR CH"},{"name":"DR. SAFDAR HUSAIN BUKHARI"},{"name":"DR. ANSAR MEHBOOB"},{"name":"DR. SAJID MEHMOOD KHAN"},{"name":"DR. MAZHAR ABBAS KHAN"},{"name":"DR. ASMA MALIK"}]

so where lies the problem and what is the solution.

2
  • 1
    where lies the problem...where the error message tells you it lies: " mysqli::real_escape_string(): Passing null to parameter #1 ($string) of type string is deprecated" . If you have error reporting enabled then any warning/error will cause PHP to output a HTML-formatted error message. Of course, this is not JSON. In a live environment you shouldn't really have reporting enabled in the output, it should be logged to a file instead - you can configure PHP to do this.
    – ADyson
    Commented Jul 1 at 9:32
  • 1
    $this->input->post('check') will return null if there is no element with a key of check in the $_POST payload. Did you print out $this->input->post() to see what is actually hitting your controller? I don't see check in your view. I do see refBy. How exactly are you expecting ->like() to inject your 2d array of submission data?
    – mickmackusa
    Commented Jul 1 at 9:32

2 Answers 2

1

You are passing a 2d array as a parameter of a method (like()) which is expecting a string.

Assuming your intention is to search for all names similar to the name column values in your 2d json payload AND your typescript is expecting to receive a flat array of values, this should do the trick:

public function get_doctor()
{
    echo json_encode(
        $this->customers->get_doctor(
            array_column(
                json_decode(
                    $this->input->post('check')
                ),
                'name'
            )
        )
    );
}
function get_doctor(array $names = []): array
{
    return array_column(
        array_reduce(
            $names,
            fn($qb, $name) => $qb->or_like('name', $name),
            $this->db->select('name')
        )
        ->get('doctors')
        ->result(),
        'name'
    );
}

This should render a query resembling (I adjusted the compiled query output from a PSQL project that I had on hand):

SELECT `name`
FROM `doctors`
WHERE `name` LIKE '%SELF%' ESCAPE '!'
OR  `name` LIKE '%DR. ASIM IRSHAD%' ESCAPE '!'
OR  `name` LIKE '%DR. ABDUL HAMEED KHAN%' ESCAPE '!'
OR  `name` LIKE '%DR. MUHAMMAD SAFDAR CH%' ESCAPE '!'
OR  `name` LIKE '%DR. SAFDAR HUSAIN BUKHARI%' ESCAPE '!'
OR  `name` LIKE '%DR. ANSAR MEHBOOB%' ESCAPE '!'
OR  `name` LIKE '%DR. SAJID MEHMOOD KHAN%' ESCAPE '!'
OR  `name` LIKE '%DR. MAZHAR ABBAS KHAN%' ESCAPE '!'
OR  `name` LIKE '%DR. ASMA MALIK%' ESCAPE '!'

Topically related content:

3
  • sorry, i get error get_doctor?query=d 500(internal server error). i think there is some role of php version as i am using codegniter3 and php version 5 on localhost and typeahead is working well but on live server where php is 8.2 this is not working. actually css of input is also being posted that is why it is included in json data. so any solution please
    – ieesab
    Commented Jul 3 at 8:34
  • What version of of CodeIgniter3? What version of PHP5? Please provide the error log message from the server (500 tells me nothing). print_r() in your question indicates a json string.
    – mickmackusa
    Commented Jul 3 at 10:34
  • thanks for your help my problem is some how solved and there was very subtle flaw in my js code in views file this was probably space or indentation caused by upload process. i had almost 4 places where i was using this typeahead and all four were not working. i was trying different codes from net and applying it with commentin and decommenting of my previous code and during the process of decommenting it became function in case of get_doctor. other places were still not working and error was similar as shown in console log. then i copied the code from here to other places and all
    – ieesab
    Commented Jul 4 at 16:40
0

i had some faults in my code that is why i was not getting the results.

first i was using

 return $.get("<?php echo base_url('get_testname'); ?>", { query: query }, function (data) {

which should be $.post() so i changed to

return $.post("<?php echo base_url('get_testname'); ?>", { query: query }, function (data) {

secondly i was using check in my controller method instead of query so i changed the check to query

public function get_doctor()
{
    $query = $this->input->post('query');
    $data = $this->customers->get_doctor($query);
    echo json_encode( $data);
}
1
  • i had some faults in my code ...yes that's generally why people ask questions here ;-) . Glad you found a solution
    – ADyson
    Commented Jul 9 at 19:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.