0

I know that zend framework have some easy ways to do this, but i didn't found a good answer or tutorial to do things according to zend.

Some answers show how to do without JSON, others without Jquery View Helper, and others without using zend framework.

The thing is too simple We have 2 Selects: group and subgroup When I change group, the subgroup is populated.

This is the action:

public function getSubgroupAction(){

    $model = new MySubgroupModel();
    $id = $this->getRequest()->getParam('id');
    $where = "id = $id";
    $data = $model->fetchAll($where)->toArray();

    $json = Zend_Json::encode($data);
    return $json;
}

And this is the form

public function init(){

    $model = new MyGroupModel();
    $options = $model->fetchAll();
    // $options receive a query with id and name

    $group = new Zend_Form_Element_Select('group');
    $group->setLabel('Chose Group')
            ->addMultiOptions($options)
            ->setRequired();

    $subgroup = new Zend_Form_Element_Select('subgroup');
    $subgroup->setLabel('Chose Subgroup')
            ->setRequired();
}

I want to know where to put on form the request to 'getSubgroupAction' or what i have to do using jquery view helper to get the json response from the action.

Note: This is not a working example.

I antecipate my thanks for the attention and the help. tkzalot.

1
  • no one can give an answer to this question? I am also interested in the answer! Commented Jun 23, 2011 at 8:26

1 Answer 1

0

First in your Controller in init change your context to json.

$ajaxContext = $this->_helper->getHelper('AjaxContext');
$ajaxContext->addActionContext('getSubgroup', 'json')       
->setAutoJsonSerialization(false) // or true to automatically do it for you
->initContext();

If your only going to always render json then disable the layout.

$this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

In your getSubgroup action after you hit you DB and get the data you want encode the json and echo.

echo Zend_Json_Encoder::encode($result);

If you scroll to this action json should be displayed in your browser.

With ajax you can always go and get the json response to display where ever you want. There are other ways of doing this instead of using a controller to send the json response but this is probably the easiest way to get your link working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.