【问题标题】:Using javascript to assign a php variable使用javascript分配一个php变量
【发布时间】:2012-10-10 08:15:36
【问题描述】:

我从 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() 方法的值,但是现在我需要调用 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 读取该会话并取消设置我认为这不会产生太多安全问题,因为会话将存在不到一秒钟。

【问题讨论】:

  • PHP 发生在服务器端。 Javascript发生在客户端。您不能从 javascript 输出可执行的 PHP。不可能。您可以使用 AJAX 来实现您想要的效果,或者使用您需要的所有数据预先填充 javascript。
  • 您拥有的代码示例在硬编码的 HTML 上调用 $this-&gt;db-&gt;escape('&lt;a class="category" onclick="step2(),这没有意义,您为什么要 db 转义 HTML?我很确定转义是为了防止 SQL 注入

标签: php javascript html codeigniter


【解决方案1】:

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

【讨论】:

  • 我从未使用过 AJAX,这会对我的代码产生很大影响吗?另一方面,如果 js 可以创建这些临时会话是可能的吗?
  • 是的,它会对您的代码产生很大影响。让我举一个小例子。
  • 如果是这样的话,这是否会刷新页面,那么它并不是很有用。是否可以像我尝试过的那样在 onclick 事件上调用 php,或者我可以使用 onclick 调用 javascript:step2() 方法并调用 ajax 来设置一个会话变量,然后我可以用 php 读取?
  • @kellax 对不起,我不明白你的意思。我写的代码不会刷新页面。 AJAX 调用可以在不刷新页面的情况下设置会话变量。您应该阅读一些 AJAX 教程并了解“客户端 JS 和 PHP 之间的隔离墙”
  • 好吧,我需要学习如何使用从 onclick 调用的 ajax 来设置会话,然后我可以从 php 中读取它,我认为这将是我希望的最好和最简单的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多