我正在尝试在我的cakephp 2.5项目中执行搜索表单。 这是我在哪里创建表单的视图:
<?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();
?>
我的产品控制器:
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'));
}
我有一个complete_search.ctp来显示结果。
问题是$keywordSearch
和$keywordCity
总是空的......我不知道为什么。我的表格有什么问题?
答案 0 :(得分:1)
从
更改此内容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';
}
到此
$keywordSearch = 'test';
$keywordSearchCity = 'testCity';
if (!empty($this->request->data['Product']['searchName']) && !empty($this->request->data['Product']['searchCity'])) {
$keywordSearch = $this->request->data['Product']['searchName'];
$keywordSearchCity = $this->request->data['Product']['searchCity'];
}