【发布时间】: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->escape($category););"这通过 step2() 方法的值,但是现在我需要调用 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>
是否可以在 onclick 事件中使用 JavaScript 设置会话变量,例如 $_SESSION['category'] = 'category' 在 js 样式中,然后使用 php 读取该会话并取消设置我认为这不会产生太多安全问题,因为会话将存在不到一秒钟。
【问题讨论】:
-
PHP 发生在服务器端。 Javascript发生在客户端。您不能从 javascript 输出可执行的 PHP。不可能。您可以使用 AJAX 来实现您想要的效果,或者使用您需要的所有数据预先填充 javascript。
-
您拥有的代码示例在硬编码的 HTML 上调用
$this->db->escape('<a class="category" onclick="step2(),这没有意义,您为什么要 db 转义 HTML?我很确定转义是为了防止 SQL 注入
标签: php javascript html codeigniter