简体   繁体   English

将 php var 传递给 javascript

[英]pass php var to javascript

So, I run this javascript.所以,我运行这个 javascript。 this code gets the html generated by cart.php此代码获取由 cart.php 生成的 html

$.ajax({
    type: "GET",
    url: "/cart/cart.php",
    async: false,
    dataType: "html",
    success: function(html){
            $('#cart_content').html(html);          
    }

QUESTION.问题。 how can get the value of a variable in cart?php ?如何获取购物车中变量的值?php?

I would love something like this: $('#cart_content').html($myvar);我会喜欢这样的: $('#cart_content').html($myvar);

Personally, the easiest way is to return json, or simply echo-ing out the data you want returned.就个人而言,最简单的方法是返回 json,或者简单地回显出你想要返回的数据。 If you were to do json, change the dataType to json, and then, on cart.php, echo json_encode(array('varname'=>$myvar));如果您要执行 json,请将 dataType 更改为 json,然后在 cart.php 上, echo json_encode(array('varname'=>$myvar)); In your success function, you will be able to call that variable: $('#cart_content').html(html.varname);在您成功的 function 中,您将能够调用该变量: $('#cart_content').html(html.varname);

If you choose to go the simple route, on cart.php, just echo the data.如果您选择 go 简单路线,在 cart.php 上,只需回显数据即可。 Your success function will have it stored as html.您的成功 function 会将其存储为 html。

cart.php can return whatever you like. cart.php 可以退货。 It doesn't have to be HTML.它不一定是 HTML。 You could return just value of the variable, or send it back in a JSON object along with the rest of the result.您可以只返回变量的值,或者将其发送回 JSON object 以及结果的 rest。

You can't do it that way.你不能那样做。 You might want to parse it as xml instead.您可能希望将其解析为 xml

ie IE

cart.php would return something like: cart.php 将返回如下内容:

[...]

echo '<var>My Variable</var>';

echo '<html><![CDATA[ <p>Html stuff</p> ]]></html>';

Then your javascript might be like那么你的 javascript 可能就像

$.ajax({
    type: "GET",
    url: "/cart/cart.php",
    async: false,
    dataType: "html",
    success: function(response){

        var xmlDoc = $.parseXML(response),
        $xml = $(xmlDoc),
        $var = $xml.find("var");  // This is your variable 

        $('#cart_content').html($xml.find("html"));      
    }
});

Something like this comes to mind:想到这样的事情:

$('.add_to_cart').click(function(){
    var _item = this.id; // say you had <div id="123" class="add_to_cart"> where the item id = 123.
    $.ajax({
        type: "GET",
        url: "/cart/cart.php?add="+ _item,
        dataType: "html",
        success: function(data){
            $('#cart_content').html(data);          
        }
    });
});

cart.php file:购物车.php 文件:

$_SESSION['cart_contents'][] = $_GET['add'];
$tmp = '';
foreach($_SESSION['cart_contents'] as $item)
{
    $tmp.= '<div class="cart_item">' . $item['whatever'] . '</div>';
}
echo $tmp; // this is what is sent back to the ajax `success` function

That would allow you to click on an "Add to cart" button and tell your cart.php page to add it and return the contents of the newly populated cart back to the DOM container #cart_content这将允许您单击“添加到购物车”按钮并告诉您的cart.php页面添加它并将新填充的购物车的内容返回到 DOM 容器#cart_content

If you want to pass data to the server-side script "cart.php" then send it as a querystring parameter:如果要将数据传递给服务器端脚本“cart.php”,则将其作为查询字符串参数发送:

$.ajax({
    type: "GET",
    url: "/cart/cart.php?myvar=$myvar",
    async: false,
    dataType: "html",
    success: function(html){
            $('#cart_content').html(html);          
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM