Question

I am using latest(2.1.1)version of cakePhp. I was trying to implement ajaxHlper(http://www.cakephp.4uk.pl/) . but stuck in between.

 <head>
<script type="text/javascript">
     $(document).ready(function(){
            $("#EmployeeAddForm").validate();
     });            

</script>   
</head>

<div class="employees form">
<?php echo $this->Form->create('Employee');?>
<fieldset>
    <legend><?php echo __('Add Employee'); ?></legend>
<?php
    echo $this->Form->input('first_name');
    echo $this->Form->input('last_name');
    //echo $this->Form->input('age');
    echo $this->Form->input('age', array('class' => 'required number'));
    echo $this->Form->input('sex');
    echo $this->Form->input('Adress.first_line');
    echo $this->Form->input('Adress.second_line');
    echo $this->Form->input('Adress.city');
    echo $this->Form->input('Adress.state');
    echo $ajax->autoComplete('Department.name', '/ajax/autoComplete')
?>
</fieldset>
   <?php echo $this->Form->end(__('Submit'));?>
 </div>
   <div class="actions">
<h3><?php echo __('Actions'); ?></h3>
<ul>

    <li><?php echo $this->Html->link(__('List Employees'), array('action' => 'index'));?></li>
    <li><?php echo $this->Html->link(__('List Adresses'), array('controller' => 'adresses', 'action' => 'index')); ?> </li>
    <li><?php echo $this->Html->link(__('New Adress'), array('controller' => 'adresses', 'action' => 'add')); ?> </li>
</ul>

This is my add.ctp file where Department.name I am trying to autocomplete using ajaxHelper.

In my EmployeesController.php I have autocomplete function which is like

 function autoComplete() {
    echo $this->params['url']['q'] . "---";
    $this->loadModel("Department");
    $this->set('departments', $this->Department->find('all', array(
        'conditions' => array(
            'Department.name LIKE ' => '%'.$this->params['url']['q'].'%'
        ),
        'limit' => $this->params['url']['limit'],
        'fields' => array('name')
    )));
    $this->layout = 'ajax';
} 

It's not working. What could be the mistake I am doing? it gives me following error:

Notice (8): Undefined variable: ajax [APP\View\Employees\add.ctp, line 84] Fatal error: Call to a member function autoComplete() on a non-object in C:\xampplite\htdocs\cakephp\app\View\Employees\add.ctp on line 84

No correct solution

OTHER TIPS

Assuming you have added the helper to the $helpers array of your controller, you have to access the helper with $this->Ajax->autoComplete() instead of $ajax->autoComplete().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top