Cakephp-使用搜索按钮创建搜索字段
我需要在按钮上执行“onclick”操作,以便重定向到类似以下内容:Cakephp-使用搜索按钮创建搜索字段,php,forms,cakephp,search,redirect,Php,Forms,Cakephp,Search,Redirect,我需要在按钮上执行“onclick”操作,以便重定向到类似以下内容: location/[textfield_data] 我使用的是cakephp,此时我最接近的地方是这个 echo $this->Form->input('link', array('label' => false, "class" => "form-control input-medium", "placeholder" => __('Procurar'))); echo $this-
location/[textfield_data]
echo $this->Form->input('link', array('label' => false, "class" => "form-control input-medium", "placeholder" => __('Procurar')));
echo $this->Form->button('', array('class' => 'btn btn-primary icon-search icon-white' ,'onclick' => "location.href='/'"));
通过我的代码,cakephp检索到以下内容:
/[view]?data%5Blink%5D=
[view]是我所在的当前页面
找到了解决方案
我这样找到了解决办法
echo $this->Form->input('link', array('label' => false, "class" => " form-control input-medium", "placeholder" => __('Procurar'), 'id' => 'search'));
echo $this->Form->button(null, array('class' => 'btn btn-primary icon-search icon-white' ,'onclick' => "location.href='/mywantedurl/'+document.getElementById('search').value;"));
请注意,我没有使用任何表单->创建或表单->结束,否则它将无法工作。我使用这样的代码
echo $this->Form->input('link', array('label' => false, "class" => "form-control input-medium", "placeholder" => __('Procurar'), 'id' => 'search-field'));
echo $this->Form->button('', array('class' => 'btn btn-primary icon-search icon-white' , 'id' => 'search-button'));
$this->Js->get('#search-button');
$this->Js->event('click', 'edit()');
$this->Js->buffer
(
"function edit()
{
search_term = this.document.getElementById('search-field').value;
if(search_term)
window.location = \"/index.php/location/\" + search_term;
return false;
}"
);
为什么需要使用JavaScript?只需使用
GET
方法即可。您应该使用GET
而不是POST
请求进行任何方式的搜索,以便可以对请求进行图书标记等
您可以使用$this->request->query
而不是$this->request->data
来实现这一点
<?php
// app/View/Locations/index.ctp
echo $this->Form->create('Location', array('type' => 'get'));
echo $this->Form->input('Location.keywords');
echo $this->Form->end('Search');
我非常感谢您的帮助,但是当我放置代码时,按钮没有反应,调试器中也没有JS错误。非常感谢您的帮助,但是我不想使用任何类型的GET,因为我需要一个干净的url,而不需要任何url编码。如果它作为查询字符串参数或段进入url,那么不管怎样,它都需要编码。
<?php
// app/Controller/LocationsController.php
class LocationsController extends AppController {
public function search() {
if (!isset($this->request->query['keywords'])) {
throw new BadRequestException();
}
$results = $this->Location->findByKeywords($this->request->query['keywords']);
$this->set('results', $results);
}
}