所以,我运行这个 javascript 。此代码获取 cart.php 生成的 html
$.ajax({
type: "GET",
url: "/cart/cart.php",
async: false,
dataType: "html",
success: function(html){
$('#cart_content').html(html);
}
问题!如何获取 cart.php 中变量的值?
我喜欢这样的东西:$('#cart_content').html($myvar);
最佳答案
就个人而言,最简单的方法是返回 json,或者简单地回显您想要返回的数据。如果您要执行 json,请将 dataType 更改为 json,然后在 cart.php 上,
echo json_encode(array('varname'=>$myvar));
在您的 success 函数中,您将能够调用该变量:$('#cart_content').html(html.varname);
如果您选择走简单的路线,在 cart.php 上,只需回显数据即可。您的成功函数会将其存储为 html。
关于php - 将 php var 传递给 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7034366/