简体   繁体   English

从视图中将CakePHP交叉发布到不同的控制器

[英]CakePHP Cross Posting to Different Controllers, from Within a View

I have a 'jobs' controller which allows users to assign tasks/jobs to each other. 我有一个“工作”控制器,允许用户相互分配任务/工作。 Each of these jobs has a 'client' which is a foreign key. 这些工作中的每一个都有一个“客户”,这是一个外键。

Within my 'add' view for the jobs controller, I've added a checkbox and another field to allow the user to add a client from within the 'add' job form. 在作业控制器的“添加”视图中,我添加了一个复选框和另一个字段,以允许用户从“添加”作业表单中添加客户端。 Basically, I need an ajax request that sends the entered client name, to the 'clients' controller, who will then add the new client to the database, and in turn return a new list of clients, for me to update my drop-down list. 基本上,我需要一个ajax请求,将输入的客户端名称发送到“客户端”控制器,然后控制器将新客户端添加到数据库中,并依次返回新的客户端列表,以供我更新下拉列表清单。

I'm not sure where to begin with this, as I've not used AJAX from a PHP environment before and I'm not sure how I should structure it, or how I should begin processing it. 我不确定从哪里开始,因为我以前从未在PHP环境中使用过AJAX,也不确定如何构造它,或者应该如何开始处理它。

I've hammed a solution out. 我已经提出了解决方案。 It's not very elegant, but it works. 它不是很优雅,但可以。

At the bottom of my View: 在我的视图底部:

echo $this->Html->scriptBlock(
            'function sendAjaxRequest(){
            $.ajax({
               type: "POST",
                url: "' . $this->Html->url(array('controller' => 'clients', 'action' => 'add_external')) . '",
                data: "name="+$(\'#JobNewClientName\').val(),
                success: function(msg){
                    //parse and sort the entries alphabetically
                    msg = $.parseJSON(msg);
                    var values = [];
                    for(var i in msg) {
                        values.push({ key: i, value: msg[i] });
                    }
                    values.sort(function(a, b) {return a.value.toLowerCase().localeCompare(b.value.toLowerCase())});
                    var str = values.map(function (kvp) { return kvp.value; }).join(\'\n\');
                    //remake the selectbox options
                    var options = \'<option value=""></option>\';
                    $.each(values, function(i){
                        options += \'<option value="\'+values[i].key+\'">\'+values[i].value+\'</option>\';
                    });        
                    $(\'#JobClientId\').html(options);
                }
            });
     }');

Controller: 控制器:

function add_external() {
        $this->autoRender = false;        
        $this->Client->create();
        $this->Client->set('name', $_POST['name']);
        $this->Client->save();
        echo json_encode($this->Client->find('list', array('order' => array('Client.name' => 'ASC'))));     
    }

Essentially, the add_external() function takes a posted name from the ajax and adds it to the database. 本质上, add_external()函数从ajax获取发布的名称,并将其添加到数据库中。 Then it json_encode s all clients. 然后它json_encode的所有客户端。

This JSON object is then parsed and sorted in the javascript success statement, this sorting retains the order as defined by the controller, ie alphabetically by value (not key). 然后,此JSON对象在javascript成功语句中进行解析和排序,这种排序将保留控制器定义的顺序,即按字母顺序按值(不是键)定义。

Next, I am able to create a brand new options variable containing the same markup that the select box requires. 接下来,我将能够创建一个全新的options变量,其中包含与选择框相同的标记。 Then all I need to do is switch them out. 然后,我需要做的就是将它们关闭。

If anybody can think of a more elegant solution, I'd be happy to award them the acceptance. 如果有人能想到一个更优雅的解决方案,我很乐意授予他们认可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM