我需要在按钮上执行"点击"操作,所以我被重定向到这样的事情:
location/[textfield_data]
- http://snag.gy/wz395.jpg
正在使用 cakephp,此时我到达的最接近的是这个。
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=
[视图] 是我所在的当前页面。
找到解决方案
我以这种方式找到了解决方案。
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;"));
请注意,我没有使用任何表单>创建或表单>结束,否则它将不起作用。
为什么需要使用 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');
然后在相应的控制器中:
<?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);
}
}
我不知道您的数据库表的实际模式或模型名称,但上面的内容应该为您指明正确的方向。
我使用这样的代码
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;
}"
);