使用javascript分配php变量

Ste*_*ess 2 html javascript php codeigniter

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

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

我尝试了两件事:

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

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

2)我将step2()更改为step2(类别)然后使用,onclick ="step2(db-> escape($ category););" 这将值传递给step2()方法然而现在我需要调用< - 我需要打印出该字符串到php然而我不知道如何解决这个问题考虑PHP - > JS是可行的,因为它的服务器客户关系,但回来..

$ this-> db-> escape()是一个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>
Run Code Online (Sandbox Code Playgroud)

是否有可能在onclick事件中设置一个带有JavaScript的会话变量,在js样式中说$ _SESSION ['category'] ='category'然后用php读取该会话并取消设置它我不认为这会创建很多安全性因为会话将存在不到一秒钟的问题.

Jua*_*des 6

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

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

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

... onclick="$.ajax('/category.php').done(function(data) {step2(data.category);})"
Run Code Online (Sandbox Code Playgroud)

如果它不是HTML中的JS,哪个会好得多

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

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