Аякс с Zendframework - PullRequest
       21

Аякс с Zendframework

1 голос
/ 24 февраля 2010

Друзья

Как я интегрирую Ajax с Zendframework. Мне нужны хорошие уроки.

Ответы [ 2 ]

3 голосов
/ 25 февраля 2010

Поместите это в свой контроллер

public function init()
{
    $contextSwitch = $this->_helper->getHelper('contextSwitch');
    $contextSwitch->addActionContext('test', 'json')
                  ->initContext();
}

создать тестовое действие

public function testAction()
{
    $this->view->var1 = "I'm testing";
}

Затем используйте jquery для выполнения запроса в вашем представлении

<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>
1 голос
/ 23 июня 2010

Вы можете использовать что-то вроде этого в вашем контроллере:

    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') );
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...