views:

70

answers:

3

Right now, what i'm trying to do is to replace a label on the front page with a block of html. Right now, the page basically has:

<label id="replace"></label>

the js currently has:

$(document).ready(function(){
  $("#replace").load('/test');
});

the Zend class function has:

public function indexAction(){
  $this->_helper->layout()->disableLayout();  
  $this->_view->message = "This is from TestController index";
}

and finally the index.phtml template simply has:

<?php echo $this->message;?>

Right now, I want to change the code around so that instead of just replacing that label with the same message, it would do a POST where the function will pull out a parameter, do something (like for instance, go to the database and pull something out with the POST parameter) and then return the message.

I've tried editing the js so that it would look like:

$.post('/test', {param : "test_param"},
function(data) {$("#replace").html(data);});

or

$.ajax({
  type: 'POST',
  url: '/test',
  data: "{param:test_param}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {$("#replace").html(data);}
});

and neither worked. I took a step back and tried to replicate the .load functionality and do:

$.ajax({
  url: '/test',
  success: function(data) {
    $('#replace').html(data);
    alert('Load was performed.');
  }
});

and it doesn't work either.

Anyone have any tips on how to go about doing this?

A: 

Are you sure that you can call /test directly with out using ajax? Also see what is the absolute url you are calling to. Try adding error callback and see it works, it will give you the error response which will help you fix the problem.

Teja Kantamneni
I've changed the code a bit so that I could see if there are any errors popping up. I added: "error: function(msg) {alert('Error occured with #replace: '.msg);}" inside the ajax call and Firebug's reporting an error: "missing } after property list" on that line. It looks like i'm closing my parens properly, so i'm not sure what's going on there.
Matthew
missing comma between success function and error function
thetaiko
also in `alert('Error occured with #replace: '.msg)` before msg it should be a `+`
Teja Kantamneni
Okay, so an alert box is popping up. In msg.responseText, i'm getting: "<!--<p>This is from testController/index.phtml</p>-->" which is the actual text that I used to get with .load(). So I guess that it is returning the html from the function, but adding a second parameter to the error function reveals that it's being classified as a parserror. Do you have any ideas on that?
Matthew
A: 

The original .post code you have looks right

$.post('/test', { param: "test_param" }, function(data) {
 $('#replace').html(data);
});

You can also try looking at your browsers JavaScript console to see if any errors are being reported.

thetaiko
A: 

Is your controller set to auto JSON encode the view parameters?

Either way I think your supposed to access it like

{ success: function(data) {$("#replace").html(data.message); }

Ballsacian1