0

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);

5
  • 1
    I have a feeling you're not well-versed in Javascript and asynchronous programming... Commented Aug 12, 2011 at 0:13
  • Your question is really hard to understand, you may want to rephrase it.. Commented Aug 12, 2011 at 0:14
  • At a very high level, you probably want to look into JSON. Commented Aug 12, 2011 at 0:15
  • @Ates Goral: That just doesn't make any sense with what the author is trying to do. HTML as a response is perfectly valid in some situations. Commented Aug 12, 2011 at 0:17
  • 1
    @Stefan - If someone were passing a PHP variable back to the client page to process, JSON could be entirely appropriate. HTML, however, could be appropriate if the OP could return an HTML fragment to the handler, but that's not apparently what they want to do. Commented Aug 12, 2011 at 0:19

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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

Comments

0

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

Comments

-1

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);          
    }

4 Comments

He wants to pass the PHP variable value TO the handler, not the other way around.
I think the OP wants the opposite: sending data from PHP to JS. JSON comes to mind.
I don't think you understand the question.
Yep, you are right. His question was so unintelligible that I lost track by the time I finished reading it. If I had enough points I would downvote my answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.