CodeIgniter中自动补全功能异常问题(附Welcome控制器代码)
Alright, let's dig into why your CodeIgniter autocomplete isn't working—there are a few clear issues in your code, plus some common gotchas to check:
1. You're checking for AJAX requests the wrong way
That line if('IS_AJAX') { is the biggest culprit here. You're just checking if a string is "truthy" (which it always is), but that's not how you detect AJAX in CodeIgniter. The framework has a built-in method for this, so replace that line with:
if ($this->input->is_ajax_request()) {
This properly checks if the request was sent via AJAX, so your JSON output only gets sent when it's supposed to.
2. print_r($query) is breaking your JSON output
You've got a print_r($query) in there that dumps raw debug text before your JSON. Even if everything else works, this will make your response invalid JSON—frontend JavaScript can't parse that, so your autocomplete won't get any usable data. Delete that line (or comment it out when you're done testing).
3. You're handling user input unsafely (and risking errors)
Using $_GET['term'] directly isn't just bad practice—it'll throw an error if the term parameter is missing from the request. Use CodeIgniter's input class to safely grab the value, and add a check for empty input:
$keyword = trim($this->input->get('term', TRUE)); // TRUE turns on XSS filtering if (empty($keyword)) { echo json_encode([]); return; }
4. Double-check your model setup
First, make sure you've loaded the Main_tutorial model somewhere—either in your controller's constructor:
public function __construct() { parent::__construct(); $this->load->model('Main_tutorial'); }
Or right before you call the lookup method. Then, verify your model's lookup method is actually returning the data you need. It should look something like this (using CodeIgniter's query builder to avoid SQL injection):
public function lookup($keyword) { $this->db->like('tutorial_name', $keyword); $result = $this->db->get('your_tutorials_table'); // Replace with your actual table name return $result->result_array(); }
5. Don't forget the frontend
Even if your backend is fixed, the frontend might be misconfigured:
- Make sure your AJAX request is sending a parameter named
term(since your code looks for that). - Use
site_url('welcome/lookup')to get the correct URL for your controller method (hardcoding URLs can cause issues if your base URL changes). - If you're using jQuery UI Autocomplete, your JS should look something like this:
$('#your-input-id').autocomplete({ source: "<?php echo site_url('welcome/lookup'); ?>", minLength: 1 });
Here's the cleaned-up controller code
Putting all these fixes together, your lookup method should look like this:
public function lookup() { $keyword = trim($this->input->get('term', TRUE)); $data['response'] = 'false'; $data['auto_com'] = []; if (!empty($keyword)) { $query = $this->Main_tutorial->lookup($keyword); if (!empty($query)) { $data['response'] = 'true'; foreach ($query as $row) { $data['auto_com'][] = $row['tutorial_name']; } } } if ($this->input->is_ajax_request()) { echo json_encode($data['auto_com']); exit; // Stop any extra output from being sent } else { // Handle non-AJAX requests (e.g., show 404) show_404(); } }
内容的提问来源于stack exchange,提问作者omkara

