3

So I have a function that uses jQuery's $.post mechanism to fill in a div with contents from another page. I want to fire this method in the very beginning to show the content in the div right away. To do this I call the method in the document.ready script.

This is how the code looks:

    function OnloadFunction()
{
    alert("HELP!");
    var url = "<?php echo $this->url('public', array('id'=>'ajax')); ?>";
     $.post(url, {contentVar:cv}, function(data){
          $("#shoppingCart").load(data).show();
     });
}

//when document loads, do the following
$(document).ready(function(){
     OnloadFunction();
})

When I load the page, the alert saying "HELP!" shows up, but the $.post function is not making any difference and the div is not filled with the contents from the url.

P.S This shouldn't make a difference but I am using ZF2 (hence the url('public', array('id'=>'ajax')); ?>)

Any help would be appreciated :)

Thanks!

Steve-o-graph
  • 67
  • 1
  • 6
  • 1
    What URL does the ajax post to? Is the post a success or a 404? What is `cv`? What does debugging in the callback show for `data`? Is there an element on your page with id `shoppingCart`? – jbabey Oct 02 '13 at 12:44

2 Answers2

2

The load function takes a URL as an argument, makes an XHR request for it, and then populates the element with the response.

Presumably data is not a URL.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

You need to use .html() instead of .load(), assuming data is html content

 $("#shoppingCart").html(data)
Satpal
  • 132,252
  • 13
  • 159
  • 168