简体   繁体   English

将数组从视图传递到控制器cakephp

[英]passing arrays from the view to the controller cakephp

Here is a part of my view(a javascript method that is executed on a button click): 这是我的观点的一部分(单击按钮即可执行的javascript方法):

function assign()
{
    var links_list1 = [];
    var links1 = document.getElementById('moderatorUsers').getElementsByTagName('a');
    for(var a in links1) {
        if(typeof links1[a] == undefined) continue;
        links_list1.push(links1[a].innerHTML);} var str1 =links_list1.toString();
    var moderators = str1.split(',');

    var links_list2 = [];
    var links2 = document.getElementById('editorUsers').getElementsByTagName('a');
    for(var a in links2) {
        if(typeof links2[a] == undefined) continue;
        links_list2.push(links2[a].innerHTML);} var str2 =links_list2.toString();
    var editors = str2.split(',');

    var links_list3 = [];
    var links3 = document.getElementById('jEditorUsers').getElementsByTagName('a');
    for(var a in links3) {
        if(typeof links3[a] == undefined) continue;
        links_list3.push(links3[a].innerHTML);} var str3 =links_list3.toString();
    var jEditors = str3.split(',');             
}

Here is the controller method i need to call using the 3 arrays from the javascript(moderators, editors,jEditors): 这是我需要使用javascript中的3个数组(控制器,编辑器,jEditors)调用的控制器方法:

  function insertPos($moderators,$editors,$jEditors){
        $account = new Account();
        $account->insertPos($moderators,$editors,$jEditors);
    }

I need to know how to execute the controller method insertPos($moderators,$editors,$jEditors) using the 3 arrays in the javascript method... 我需要知道如何使用javascript方法中的3个数组执行控制器方法insertPos($ moderators,$ editors,$ jEditors)...

I used this to send the arrays in the javascript like you told me: 我用它像您告诉我的那样在javascript中发送数组:

$.post('http://localhost/cakephp/Accounts/insertPos', {
            data: {
                'moderators': moderators,
                'editors': editors,
                'jEditors': jEditors
            }
        });

and in the controller i try to access my arrays like this: 在控制器中,我尝试像这样访问我的数组:

public function insertPos() {
        if (!empty($this->request->data)){
        print_r($this->request->data);
        $moderators = $this->request->data['moderators'];
        $editors = $this->request->data['editors'];
        $jEditors = $this->request->data['jEditors'];
        $account = new Account();
        $account->assignPos($moderators,$editors,$jEditors);
        }
    }

the part inside the if(!empty($this->request->data)) is never executed so that means the arrays have not been sent to the controller.... where is the problem? if(!empty($this->request->data))内部的部分永远不会执行,这意味着尚未将数组发送到控制器。...问题出在哪里?

thank you.... 谢谢....

It looks like you're trying to access a controller class directly. 看来您正在尝试直接访问控制器类。 This is not how CakePHP works. 这不是CakePHP的工作方式。 You have to go through the dispatch process. 您必须完成调度过程。 Please read: http://book.cakephp.org/2.0/en/getting-started/a-typical-cakephp-request.html 请阅读: http : //book.cakephp.org/2.0/en/getting-started/a-typical-cakephp-request.html

That said, the way you would POST to a CakePHP url is thusly: 也就是说,发布到CakePHP网址的方式是这样的:

// POST to the AccountsController's insertPos method
$.post('/accounts/insertPos');

To pass data, pass it in the data option as specified with jQuery, prefixed with 'data', like data[moderators] so it ends up in Cake's data variable. 要传递数据,请将其传递到jQuery指定的data选项中,并以'data'开头,例如data[moderators]这样它就以Cake的data变量结尾。

$.post('/accounts/insertPos', {
  data: {
    'data[moderators]': moderators,
    'data[editors]': editors,
    'data[jEditors]': jEditors
  }
});

The data will now end up in $this->request->data in Cake. 数据现在将以Cake中的$this->request->data结尾。

Looking at your insertPost() method, though, you are passing them simply as parameters, so instead you would write your ajax like so 但是,看着您的insertPost()方法,您只是将它们作为参数传递,因此您应该像这样编写ajax

// POST is unnecessary here, since you aren't POSTing data
$.get('/accounts/insertPos/'+moderators+'/'+editors+'/'+jEditors);

You will probably need to stringify your JavaScript arrays and use json_decode in your inserPos method to convert them to PHP objects, since you can't just pass arrays from JavaScript to PHP. 您可能需要对JavaScript数组进行stringify处理,并在inserPos方法中使用json_decode将其转换为PHP对象,因为您不能仅将数组从JavaScript传递给PHP。

don't use array notation in data parameter; 不要在data参数中使用数组符号; just use keys like this: 只需使用如下键:

$.post('/accounts/insertPos', {
  data: {
    'moderators': moderators,
    'editors': editors,
    'jEditors': jEditors
  }
});

and in your controller access it as $this->request->data[key] not $this->request->data->data[key] 并在您的控制器中将其作为$this->request->data[key]访问,而不是$this->request->data->data[key]

in the view replace your assign function with the following: 在视图中,将您的assign函数替换为以下内容:

var moderators, editors, jEditors;

function assign()
{
    var links_list1 = [];
    var links1 = document.getElementById('moderatorUsers').getElementsByTagName('a');
    for(var a in links1) {
        if(typeof links1[a] == undefined) continue;
        links_list1.push(links1[a].innerHTML);} var str1 =links_list1.toString();
    moderators = str1.split(',');

    var links_list2 = [];
    var links2 = document.getElementById('editorUsers').getElementsByTagName('a');
    for(var a in links2) {
        if(typeof links2[a] == undefined) continue;
        links_list2.push(links2[a].innerHTML);} var str2 =links_list2.toString();
    editors = str2.split(',');

    var links_list3 = [];
    var links3 = document.getElementById('jEditorUsers').getElementsByTagName('a');
    for(var a in links3) {
        if(typeof links3[a] == undefined) continue;
        links_list3.push(links3[a].innerHTML);} var str3 =links_list3.toString();
    jEditors = str3.split(',');             
}

Good luck 祝好运

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

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