tags:

views:

26

answers:

2

Friends,

How I integrate Ajax with zendframework.I need some good tutorials.

A: 

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>
Keyne
A: 

Do you want to use Zend_Form, or just plain ajax?

  • If so, you can start with addActionContext to determine if you need to use or not a Layout.
  • Then you must learn how to return data in JSON, to parse it with jQuery, you can use something like this on your controller:

    public function myCoolFunctionAction () {

    // 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 a json script, it takes care of the header and everything else!
    return $this->_helper->json->sendJson ( array( ) );
    

    }

Then you can parse it in jqury and you are done :)

hope it helps

camilo_u