php - 使用javascript分配一个php变量

标签 php javascript html codeigniter

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

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

我尝试了两件事:

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

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

2) I changed step2() to step2(category) and then used, onclick="step2(db->escape($category););" this passes the value to the step2() method however now i would need to call <-- I would need to print out that string to php however i have no idea how to solve this considering PHP -> JS is viable as its server to client relation but back..

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

我的代码:

<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 变量,在 js 样式中说 $_SESSION['category'] = 'category' 然后用 php 读取该 session 并取消设置我认为这不会产生太多存在安全问题,因为 session 的存在时间不到一秒。

最佳答案

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 请求。

关于php - 使用javascript分配一个php变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12978927/

相关文章:

php - 如何使用 pre_get_posts 定位子 Woocommerce 类别

javascript - React Textarea 状态无法更新

javascript - 如何从 <canvas> 中的一张图像中捕获多张图像?

php - 从数据库输入后用 PHP 对 HTML 表进行排序

php - 将 PHP 中的数组索引与 array_reduce 相乘

php - Google Analytics 中的规范 URL 问题

javascript - Jeditable 不返回 "id"值

javascript - jQuery 和 Express - 混淆了成功和错误

java - 安全的 Nashorn JS 执行

javascript - 如何在 Javascript/Jquery 中添加表行时更改组合框选择上的文本框