【发布时间】:2015-09-19 08:55:47
【问题描述】:
我正在尝试将 php 脚本附加到具有 .new-session 类的 div 中的 dom,但它无法正常工作。
这是我的代码
$('select[name=quantity]').change(function() {
quantity = $(this).val();
if (quantity == 20) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 20; ?>';
}
elseif(quantity == 30) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 30; ?>';
}
elseif(quantity == 40) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 40; ?>';
}
elseif(quantity == 50) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 50; ?>';
}
elseif(quantity == 60) {
newSession = '<?php echo $_SESSION[\'product\'][\'quantity\'] = 60; ?>';
}
$.ajax({
url: "/multisite/",
dataType: "html",
success: function(newSession) {
$('.set-session').append(newSession);
}
})
});
<div class="set-session">
</div>
【问题讨论】:
-
如果有数据传回客户端,请使用
console.log(newSession) -
为什么要在 Javascript 中设置
newSession,然后使用与 PHP 的响应相同的变量?为什么不在 AJAX 调用中发送data:参数? -
您不能将 PHP 脚本附加到 DOM。 PHP 在服务器上运行,DOM 在客户端处理。您需要在服务器上运行 PHP,它应该返回您附加到 DOM 的 HTML。你的代码完全倒退了。
-
这只是为了调试还是你真的想执行
php代码?还要解释“不应该如何工作。”。
标签: javascript php jquery html ajax