php - AJAX JSON and routing in cakephp -


i trying implement search module using ajax.

there index.ctp file in items controller , have linked index.ctp file of items search.ctp file present under pages controller below:

<li><?= $this->html->link(__('search products'),['controller'=>'pages','action' => 'search']) ?></li> 

for search.ctp pages url displayed : http://onlineelectronic.com/pages/search

in search.ctp file code follows:

    <head>     <title> search results</title>     <?php echo $this->html->script('//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', array('inline' => false));?>     <script type="text/javascript">         $(document).ready(function() {             $("#submit1").click(function () {                 $.ajax({                     type: 'post',                     url: '/items/searchdata",                     data: {                         name: "search"                     },                     beforesend: function(){                         $("#resultfield").html("loading...");                     },                     success: function(response) {                         jquery('#resultfield').val(response);                     },                     error: function(response, error) {                         alert("search :error"+response.error());                     },                     datatype: 'json',                     global: false                 });                return false;             });         });     </script> </head>  <div>     <?= $this->form->create() ?>     <fieldset>         <legend><?= __('search item') ?></legend>         <?php         echo $this->form->input('search',['label'=>'search']);         ?>     </fieldset>      <?= $this->form->button('search items',['label'=>'submit1']); ?>     <?= $this->form->end() ?>  </div>  <div id="resultfield">  </div> 

in itemscontoller page searchdata method implemented this:

    class itemscontroller extends appcontroller {       public $helpers = ['form', 'html', 'time'];      public function initialize()     {         parent::initialize();         $this->loadcomponent('requesthandler');     }      public function search(){     //dummy     }      /**      *obtains search result given string.      */     public function searchdata()     {         $this->layout = 'ajax';         echo "here";         $search_data=[];         var_dump($search_data);         //$search_results = [];         if ($this->request->is('post')) {             $search_data= $this->request->data;             $search_data=implode("|",$search_data);             $search_results = $this->items->find('all', array('conditions'=>array('items.itemname like'=>"%$search_data%")));             if(!empty($search_results)) {                 $this->set(compact($search_results));                 $this->set('_serialize',array('search_results'));                 return json_encode($search_results);             }         }      }        public function beforefilter(event $event)     {         parent::beforefilter($event);          $this->auth->allow(['index', 'view','search','searchdata']);       } } 

my issue searchdata method not being called , not getting javascript errors also.how make sure method gets called on pressed after pressing button.is due url in json?

i see 2 possible problems. first in itemscontroller, have allow searchdata method

// change line $this->auth->allow(['index', 'view','search']); // $this->auth->allow(['index', 'view','searchdata']); 

also make sure, have proper jquery selector

// try change line <?= $this->form->button('search items',['label'=>'submit1']); ?> // <?= $this->form->button('search items',['id'=>'submit1']); ?> 

edit: make more corrections javascript:

  1. data passed ajax should double quoted

    data: {     name: "search" }, 
  2. add return false; end of click handler, prevent submiting form , reloading page

also note must have template searchdata in template/items/search_data.ctp


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -