Why don't you instead have the json encoding happen from a view action?

e.g. Assuming your controller is AjaxController.php:-

<?php

class AjaxController extends Zend_Controller_Action
{
...other code...
        function updateorderAction() {
                $i = 1;
                $o_divisiontable = new Division();
foreach ($this->getRequest()->getPost('divisioncontainer') AS $i_divisionid) { $o_division = $o_divisiontable->fetchRow("division_id = $i_divisionid");
                        $o_division->priority = $i;
                        $o_division->save();
                        $i++;
                }
                $this->view->result = array("result", 1);
        }
}

...and then your corresponding view action would be in your view folders ajax/updateorder.phtml

<?= Zend_Json::encode($this->result) ?>

(Note, I also added the 'getRequest()->getPost()' accessor to be more MVC-friendly :)

Cheers

But the problem is I am getting "edit.phtml" not found if I don't "die"
the action. Or will this change if I do the setHeader content-type?

/Peter



On Mon, 2007-11-26 at 07:45 +1000, Steven Brown wrote:
Peter,

This may not be the most correct way, but this is the solution I use:

Zend_Loader::loadClass('Zend_Json');
$this->_response->setHeader('content-type', 'application/json', true); $this->_response->setBody("/*-secure-\n" . Zend_Json::encode($response) .
"\n*/");

$response really can be anything, a string, an array, an object.

Cheers,
Steven

-----Original Message-----
From: Peter Lauri [mailto:[EMAIL PROTECTED]
Sent: Monday, 26 November 2007 7:17 AM
To: Zend Framework General
Subject: [fw-general] Ajax way

Hi,

I want to return json from an action, but I have hard times finding out
how to do it the best way with ZF. For the moment I do:

        function updateorderAction() {
                $i = 1;
                $o_divisiontable = new Division();
                foreach($_POST['divisioncontainer'] AS $i_divisionid) {
                        $o_division =
$o_divisiontable->fetchRow("division_id =
$i_divisionid");
                        $o_division->priority = $i;
                        $o_division->save();
                        $i++;
                }
                $a_return = array("result", 1);
                echo json_encode($a_return);
                die;
        }

But that isn't the way, is it?

/Peter





--

Simon Mundy | Director | PEPTOLAB

""" " "" """""" "" "" """"""" " "" """"" " """"" "  """""" "" "

Please support Movember today!  Visit http://www.movember.com/au/donate
Registration number for Simon 160725

202/258 Flinders Lane | Melbourne | Victoria | Australia | 3000
Voice +61 (0) 3 9654 4324 | Mobile 0438 046 061 | Fax +61 (0) 3 9654 4124
http://www.peptolab.com

Reply via email to