2

我从 envato 市场购买了 modalbox 插件,用 javascript 制作,我想用它来实现我的类别 > 子类别 > 项目。无论如何,我知道 JavaScript 变量可以使用 php 赋值,但我需要相反。

在我的第一步中,我让用户使用基本链接选择一个类别,然后 onclick 甚至将它们带到第二张幻灯片,我想在其中显示与所选该类别相关的子类别和项目。

我尝试了两件事:

1) setting onClick = "step2();<?php $chosen_cat = ' . $this->db->escape(trim($category)) . '?>"

希望我可以简单地将所选类别的名称分配给一个值,然后在 step2() javascript 方法中使用 $chosen_cat 来显示产品,但这会破坏 javascript,因为输出存在冲突;

2)我把step2()改成step2(category)然后用了,onclick="step2(db->e​​scape($category););" 这会将值传递给 step2() 方法,但是现在我需要调用 <-- 我需要将该字符串打印到 php 但是我不知道如何解决这个问题,因为 PHP -> JS 作为其服务器是可行的客户关系但回来..

$this->db->e​​scape() 是一个http://codeigniter.com/user_guide/database/queries.html codeigniter 方法。

我的代码:

<script type="text/javascript">

        function step1() {
            modalbox.show(new Element("div").insert(
                new Element("p", { "align": "justify" }).insert(
                    <?php 
                        $categories = $items;
                        $chosen_cat = '';
                    ?>
                    <?php 
                        $i = 0;
                        foreach($categories as $category => $itemarray) {
                            $i++;
                            if($i==27){
                                echo  $this->db->escape('<a class="category" onclick="step2();<?php $chosen_cat = ' . $this->db->escape(trim($category)) . '?>" href="#">'. trim($category) . '</a>');
                            }
                            else {
                                echo $this->db->escape('<a class="category" onclick="step2();" href="#">' . trim($category) . '</a>')  . '+';
                            }
                        }
                    ?>
                )
            ), {
                "title"     : "Step 1/3",
                "width"     : 800,
                "options"   : [{
                    "label"     : "Next »",
                    "onClick"   : step2
                }]
            });
        };

        function step2() {
            alert(<?php $this->db->escape($chosen_cat); ?>);
            modalbox.show([ new Element("div").insert(
                new Element("p", { "align": "justify" }).insert(
                    "If you open a modalbox when another one is opened, it will stretch to the new " +
                    "modalbox width and height and overwrite the content.<br /\><br /\>" +
                    "You can use HTML or Prototype Elements like a DIV or a TABLE."
                )
            ),
                new Element("p", { "align": "justify" }).insert(
                    "You can have multiple contents on a single modalbox by using an array like this:<br /\><br /\>" +
                    "<pre>modalbox.show([ content1, content2, ... ], options);</pre>"
                )
            ], {
                "title"     : "Step 2/3",
                "width"     : 800,
                "options"   : [{
                    "label"     : "« Previous",
                    "onClick"   : step1
                }, {
                    "label"     : "Next »",
                    "onClick"   : step3
                }]
            }); 
        };
        function step3() {
            modalbox.show([ new Element("div").insert(
                new Element("p", { "align": "justify" }).insert(
                    "If you open a modalbox when another one is opened, it will stretch to the new " +
                    "modalbox width and height and overwrite the content.<br /\><br /\>" +
                    "You can use HTML or Prototype Elements like a DIV or a TABLE."
                )
            ),
                new Element("p", { "align": "justify" }).insert(
                    "You can have multiple contents on a single modalbox by using an array like this:<br /\><br /\>" +
                    "<pre>modalbox.show([ content1, content2, ... ], options);</pre>"
                )
            ], {
                "title"     : "Step 3/3",
                "width"     : 800,
                "hideOnPageClick": false,
                "options"   : [{
                    "label"     : "« Previous",
                    "onClick"   : step2
                }, {
                    "label"     : "Finish",
                    "isDefault" : true,
                    "onClick"   : modalbox.hide
                }]
            });
        };

    </script>

是否可以在 onclick 事件中使用 JavaScript 设置会话变量,例如 $_SESSION['category'] = 'category' 在 js 样式中,然后使用 php 读取该会话并取消设置它我认为这不会产生很大的安全性会话将存在不到一秒钟的问题。

4

1 回答 1

6

JavaScript 永远不会设置 PHP 变量。JavaScript 在客户端运行,PHP 在服务器上运行。如果您需要从 JS 更改服务器上的某些内容,则必须使用 AJAX,或发送常规 HTTP 请求。

或者,您可以让 PHP 在客户端生成运行时所需的数据,通常通过打印一些 JSON 来成为 JS 变量

在您的示例中,您将需要以下内容(为简单起见,我将使用 jQuery)

... onclick="$.ajax('/category.php').done(function(data) {step2(data.category);})"

如果它不是 HTML 中的 JS,那会更好

$('#my-button').click(function(){
    $.ajax('/category.php').done(function(data) {
        step2(data.category);
    });
});

我不能给你一个 AJAX 教程,但主要是当你需要来自服务器的数据时,你会发出一个 AJAX 请求。

于 2012-10-19T16:59:20.453 回答