cakephp search form with empty variables

Wednesday, November 4, 2015

I'm trying to do a search form in my cakephp 2.5 project.
Here is my view where form is created:



<?php 
echo $this->Form->create('Product', array('action' => 'complete_search'));
echo $this->Form->input('searchName', array('label' => false, 'div' => false, 'class' => 'form-control', 'autocomplete' => 'off'));
echo $this->Form->input('searchCity', array('label' => false, 'div' => false, 'class' => 'form-control', 'autocomplete' => 'off'));
echo $this->Form->button('Search', array('div' => false, 'class' => 'btn btn-sm btn-primary'));
echo $this->Form->end();
?>


My ProductsController :



public function complete_search(){
if (!empty($this->request->data['searchName']) && !empty($this->request->data['searchCity'])) {
$keywordSearch = $this->request->data['searchName'];
$keywordSearchCity = $this->request->data['searchCity'];
}else{

$keywordSearch = 'test';
$keywordSearchCity = 'testCity';
}

$products = $this->Product->find('all', array(
'recursive' => -1,
'contain' => array(
'Brand'
),
'conditions' => array(
'AND' => array(
'Brand.active' => 1,
'Product.active' => 1,
'Product.date_discount >' => date('Y-m-d H:i:s'),
'Product.name LIKE' => '%' . $keywordSearch . '%',
'Brand.city LIKE' => '%'. $keywordSearchCity .'%'
)
),
'limit' => 200,
));

$products = $this->paginate($products);

$this->set(compact('products'));
}


And I have a complete_search.ctp to show the results.



The problem is that $keywordSearch and $keywordCity are always empty... And I don't know why. What's wrong with my form?



Thank you for your help.

0 comments:

Post a Comment