简体   繁体   English

如何将javascript var传递到ajax remoteFunction中?

[英]How to pass javascript var into a ajax remoteFunction?

I have a view where ill have 3 divs: 我认为生病的人有3个div:

Div 1: List of Brands with checkboxs. 第1分部:带有复选框的品牌列表。 Div 2: List of Categories with checkboxs. 第2分部:带有复选框的类别列表。

Div 3: List of Items. 第3分部:项目清单。

This last div will be refreshed with the all the items according to what is selected in the first two divs. 最后一个div将根据前两个div中选择的内容刷新所有项目。 At beginning it shows all the items, after we select some of the brands and/or categories and press refresh i'll want to refresh the div 3. 首先,它显示了所有项目,在我们选择了某些品牌和/或类别并按“刷新”之后,我要刷新div 3。

In Javascript I can get which of the categories/brands are selected and my biggest doubt is on how to refresh the last div... 在Javascript中,我可以获得所选的类别/品牌,而我最大的疑问是如何刷新最后一个格...

Heres what I was trying: 这是我正在尝试的:

function refresh() {
var brands= /*<code where i get all the brands selected (this will be a js array)>*/
var categories = /*<code where i get all the categories selected (this will be a js array)>*/
<?php echo $ajax->remoteFunction(array('url' => array('controller' => 'items', 
                                                      'action' => 'men', brands, categories),
                                                      'update' => 'itemsContent')); ?>
}

My problems are: - How do I pass the js vars into the php method? 我的问题是:-如何将js vars传递给php方法? - How do I receive an js array in a cakephp action? -如何在cakephp操作中接收一个js数组? Because brands and categories will be used to filter the query that produce results for the div 3... 因为品牌和类别将用于过滤产生div 3结果的查询...

You won't be able to use the $ajax helper here, since it just outputs a static script which can't be changed/influenced at "run-time" in the browser. 您将无法在这里使用$ajax帮助程序,因为它仅输出静态脚本,该脚本无法在浏览器的“运行时”中更改/影响。 It just wasn't made for something more complex than it is. 并不是因为它比它复杂。

So, you'll have to roll your own JS, which shouldn't be that hard though. 因此,您将必须使用自己的JS,但这不应该那么难。 All you need is: 所有你需要的是:

  1. a Cake action that outputs a list of items based on the data it receives (shouldn't be hard) 一个Cake动作,根据收到的数据输出一个项目列表(不难)
  2. a bit of JS that figures out which brands and categories are selected (which you already have) 一点JS指出选择了哪些品牌和类别(您已经拥有)
  3. another bit of JS that packages that data and sends it to the Cake action 另一部分JS将数据打包并发送到Cake动作
  4. another bit of JS that updates the site with the list of items you received back 另一部分JS使用您收到的项目列表更新网站

I'd take a look at jQuery's AJAX functions to accomplish #3. 我将看一下jQuery的AJAX函数,以实现#3。 If you POST the data in a format like this, it's very easily accessible in $this->data in Cake: 如果以这种格式发布数据,则可以在Cake的$this->data中轻松访问它:

{
    'data[ModelName][categories]' : categories,
    'data[ModelName][brands]'     : brands
}

Regarding your question: 关于您的问题:

"How do I pass the js vars into the php method?" “如何将js变量传递给php方法?”

You don't. 你不知道 PHP runs on the server and is already finished by the time the Javascript runs in the browser. PHP在服务器上运行,并且Javascript在浏览器中运行时已完成。 The only "communication" between JS and PHP is via standard HTTP GET and POST requests, and there it doesn't matter whether the request comes from a standard browser or JS or Flash or whatnot. JS和PHP之间的唯一“通信”是通过标准HTTP GET和POST请求进行的,该请求是否来自标准浏览器,JS或Flash或其他任何东西都没有关系。

The $ajax helper just has a bunch of pre-fabricated Javascript snippets it can put into your page, but your JS will not be able to "talk to" the $ajax helper in any way. $ajax帮助器只有一堆预制的Javascript代码片段可以放入您的页面,但是您的JS将无法以任何方式与$ajax帮助器“对话”。

I had a similar scenario to yours, and I found a few methods on the Javascript helper that are applicable. 我有一个与您相似的方案,并且在Javascript帮助器上找到了一些适用的方法。 I used codeBlock() to wrap a chunk of javascript, and event() to wire up the click event, but I'm not sure how much clearer this is than just writing the raw Javascript. 我使用了codeBlock()来包装大量的javascript,并使用event()来连接click事件,但是我不确定这比编写原始Javascript更加清晰。

I found the AJAX section of the CakePHP manual to be really helpful for getting the basic set up. 我发现CakePHP手册的AJAX部分确实对基本设置很有帮助。 Then I took the generated Javascript and made it more dynamic. 然后,我将生成的Javascript转换为动态脚本。

In this example, I'm calling the add_topic action whenever the user clicks the link. 在此示例中,每当用户单击链接时,我都会调用add_topic操作。 Every time it gets called, I increment the topicIndex variable and pass it as a parameter in the AJAX call. 每次调用它时,我都会递增topicIndex变量,并将其作为参数传递给AJAX调用。 The AJAX call returns a few rows that are inserted in the table above the link that the user clicked. AJAX调用返回用户单击链接上方表格中插入的几行。

        <tr id="add_topic_row"><td colspan="3">
            <a id="add_topic_link" href="javascript:void(0);">New Topic 
                <?php echo $html->image('icons/add32.png');?></a></td></tr>
        </table>
    </fieldset>
<?php 
    echo $form->end('Submit');
    $addTopicUrl = $html->url(array('action' => 'add_topic')) . '/';
    $script = <<<EOS
var topicIndex = $index;
var addTopicUrl = '$addTopicUrl';
addTopic = function()
{
    new Ajax.Updater(
        'add_topic_row', 
        addTopicUrl + topicIndex, 
        {
            asynchronous:true, 
            evalScripts:true, 
            insertion:Insertion.Before, 
            requestHeaders:['X-Update', 'add_topic']
        });
    topicIndex++; 
}
EOS;
    echo $javascript->codeBlock($script);
    echo $javascript->event('add_topic_link', 'click', 'addTopic();')
?>

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

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