mr.Liviu.V
mr.Liviu.V

Reputation: 162

pass php var to javascript

So, I run this javascript . this code gets the html generated by cart.php

$.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 ?

I would love something like this : $('#cart_content').html($myvar);

Upvotes: 0

Views: 155

Answers (5)

SeanCannon
SeanCannon

Reputation: 77966

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:

$_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

Upvotes: 0

Steve Robbins
Steve Robbins

Reputation: 13812

You can't do it that way. You might want to parse it as xml instead.

ie

cart.php would return something like:

[...]

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

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

Then your javascript might be like

$.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"));      
    }
});

Upvotes: 0

Tim Withers
Tim Withers

Reputation: 12059

Personally, the easiest way is to return json, or simply echo-ing out the data you want returned. If you were to do json, change the dataType to json, and then, on 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);

If you choose to go the simple route, on cart.php, just echo the data. Your success function will have it stored as html.

Upvotes: 3

Andrew Cooper
Andrew Cooper

Reputation: 32576

cart.php can return whatever you like. It doesn't have to be HTML. You could return just value of the variable, or send it back in a JSON object along with the rest of the result.

Upvotes: 0

PPrice
PPrice

Reputation: 2363

If you want to pass data to the server-side script "cart.php" then send it as a querystring parameter:

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

Upvotes: -1

Related Questions