php - CakePHP: "GET"表单在提交后不自动填充表单字段

标签 php forms cakephp query-string cakephp-2.3

我正在使用 Cake 2.3.0。如果我使用 POST 提交我的表单,则选定的表单字段会保留,但是如果我使用 GET 提交我的表单,所有表单字段都会返回到它们的默认值。

有没有办法让 GET 提交像 POST 一样工作?

这是我的 Controller :

class ListingsController extends AppController {
    public function results() {
        $conditions = array(
            'Listing.Beds >=' => $this->request->query['beds'], 
            'Listing.ListingStatus >=' => $this->request->query['status'], 
        );

        $this->paginate = array(
            'conditions' => $conditions,
        );    

        $this->set('listings', $this->paginate());
    }
}

这是我的 View 。

echo $this->Form->create(null, array(
    'controller' => 'listings', 
    'action' => 'results',
    'type' => 'get'
));

echo $this->Form->input('name');

$beds = array('1' => '1+', '2' => '2+', '3' => '3+', '4' => '4+', '5' => '5+');
echo $this->Form->input('beds', array('options' => $beds));

$status = array('Active' => 'Active', 'Pending' => 'Pending', 'ActivePending' => 'Active and Pending');
echo $this->Form->input('status', array('options' => $status));

echo $this->Form->end('Update'); 

所以基本上,如果我将 'type' => 'get' 更改为 'type' => 'post' 它就可以正常工作。但我需要能够通过 GET 执行此操作。

谢谢

最佳答案

我同意这很烦人(IMO CakePHP 应该足够聪明,可以根据“类型”自动确定从“何处”获取数据)。

您必须将请求的“查询”复制到请求的“数据”;

$this->request->data = $this->request->query;

不在我的电脑后面测试它(像往常一样,哈哈),但应该可以工作。

关于php - CakePHP: "GET"表单在提交后不自动填充表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751544/

相关文章:

javascript - 用形式替换单词(Javascript)

mysql - FROM 中的 cakePHP 子查询

php - 新条目后 AJAX 分页无法正常工作

php - 检查变量是否在数组中多次存在?

php - Ajax 表单有时有效,有时有效并刷新,有时刷新并失败......哇?

php - CakePhp 2.5 $belongsTo 有两个模型

cakephp - 基于CakePHP的项目抛出错误,提示 “return value of new by Reference is Deprecated”

php - Mailchimp 使用 php 和 drupal 8 向列表中的现有成员添加标签

php - 去除坏的 Windows 文件名字符

javascript - 对同一表单使用多个 javascript 函数