Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in CakePHP. I have a CakePHP application which has streets table (with columns street_name, house_no, district, city). I need to autocomplete these data in a form and save them in an array for further processing of the data. It should be like, the user will be typing a street name in the Street Name field of a form which has fields- Street Name, House No, District and City. After selecting a street name from auto suggestions, corresponding data (House No, District and City) will be populated in the other fields of the form.

What I have tried:

Now, over 25000 rows are loaded in the streets table which is the entire street database of a city. this table is not linked with other tables of the application through foreign keys. Street model in /Model/Street.php is following:

PHP
class Street extends AppModel {

  public function getStreet ($term = null) {
    if(!empty($term)) {
      $streets = $this->find('list', array(
        'conditions' => array(
          'street_name LIKE' => trim($term) . '%'
        )
      ));
      return $streets;
    }
    return false;
  }
}


In /Controller/StreetsController.php,

PHP
class StreetsController extends AppController {

   public function autocomplete($term){

   if ($this->request->is('get')) {
       $this->autoRender = false;
       $data = $this->Street->getStreet($term);
       $this->set(compact('data'));
       $this->set('_serialize', array('data'));

       echo json_encode($data);
       }
   }
 }


In /webroot/js/street.js file,

PHP
(function($) {
  $('#autocomplete').autocomplete({
        source: "/street.json"
  });
})(jQuery);


The input forms are in different controller. In View/Users/add.ctp file,

PHP
<fieldset>
        <legend><?php echo __('Address Data'); ?></legend>

        echo $this->Form->input('street_name', array(
                   'class' => 'ui-autocomplete',
                   'id' => 'autocomplete'));
        echo $this->Form->input('house_no');
        echo $this->Form->input('district');
        echo $this->Form->input('city');

  </fieldset>
  <?php echo $this->Form->end(__('Save')); ?>

Could anyone tell me please how to proceed further ? I found some tutorials where this autocomplete function is implemented using jQuery in CakePHP. But, it is implemented using same table, model, view and controller like cars, Car.php, /View/Cars/index.ctp , CarsController.php. which is not similar to my scenario.
Posted
Updated 22-Aug-16 22:17pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900