0

Controller A:

Zend_Controller_Front::getInstance()
        ->setParam('noViewRenderer', false);
$this->_helper->layout->disableLayout();
$form = new Form_Targets();
$this->view->form = $form;
if(!isset($form)) $form = false;

View A:

<?php echo Zend_Json::encode(array('form' => $this->form));?>

Controller B:

Zend_Controller_Front::getInstance()
        ->setParam('noViewRenderer', true);
        $this->_helper->layout->disableLayout();

        $form = new Form_Targets();
        $this->view->form = $form;
        if(!isset($form)) $form = false;
        echo Zend_Json::encode(array('form' => $form));

View B:

Blank

View script where Ajax Returned:

<div id='paz'>
    <p>Ello</p>
    <p>Ello</p>
</div>

<script type="text/javascript">
    $('#available').bind('change', function (e) 
    {
        $.getJSON('<?php echo $this->baseURL()?>/admin/ajax/target-year/year/' + encodeURIComponent($('#available').val()),
        function(data) 
        {
            alert(data);
            $("#paz").html(data.form);
        }
        );
    });
</script>

Response A:

{"form":{}}

Reponse B:

{"form":{}}

The problem is the form is not being returned...

4
  • Why you are using JSON? Maybe just send pure HTML and then you can just paste HTML to your website. Commented Nov 15, 2013 at 12:01
  • Problem is that when I use pure HTML, the getJSON function doesn't work and alert() is not run, should I use the $.ajax function instead...I tried it and it didn't even make the ajax call...maybe my syntax was wrong Commented Nov 15, 2013 at 12:03
  • 1
    Try this -> Zend_Json::encode(array('form' => $this->form->render())); so you actually have something in JSON. Commented Nov 15, 2013 at 12:07
  • That worked: used Zend_Json::encode(array('form' => $form->render())); in the controller...thanks mate Commented Nov 15, 2013 at 12:16

1 Answer 1

2

Just to not leave thread without answer:

Form has to be rendered in order to be able to encode it into JSON:

Zend_Json::encode(array('form' => $this->form->render()));

without ->render() array had form object instead of it's HTML representation.

Usually people use $this->form to render form, forgetting that it uses magic method __toString() to render form.

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.