Ajax with Zendframework
Friends,
How I integrate Ajax with zendframew开发者_JAVA技巧ork.I need some good tutorials.
Put this in your controller
public function init()
{
$contextSwitch = $this->_helper->getHelper('contextSwitch');
$contextSwitch->addActionContext('test', 'json')
->initContext();
}
create a test action
public function testAction()
{
$this->view->var1 = "I'm testing";
}
Then use jquery to perform the request in your view
<script type="text/javascript">
$(document).ready(function(){
$('#display').ajaxStart(function(){
$(this).html('Loading...');
});
str = $('#test').val();
$('#link').bind('click',function(event){
event.preventDefault();
$.post(
this.href,
{ var1: str, format: 'json' },
function(data){
$('#display').html(data.var1);
},
"json"
);
});
});
</script>
<input type="text" name="test" id="test" value="just testing" />
<p>
<a href="<?php echo $this->url(array('controller' => 'index', 'action' => 'test')); ?>" id="link">Testar</a>
</p>
<div id="display">...</div>
You can use something like this in your controller:
public function testAction() {
// Remove all of your HTML stuff
$this->_helper->layout->disableLayout();
// PHP to create an array form database or something
// This will return the info as json. It takes care of the header and everything else!
return $this->_helper->json->sendJson( array('test') );
}
精彩评论