2

I'd like to use pure Javascript httprequest to post a message to a controller/action in Zend and get back a response that I can process back in the page with a callback function, classically.

Two questions:

  1. how can I format the POST string taking into account the baseurl imposed by Zend (in the classical case I point to a php file).
  2. what should I do - the simplest possible (I understand there are several possible implementations) - in the action method so that to return a response.

Again, no jQuery, JSON, prototype or other library.

The use case is the following: I have a view with a form section. One of the inputs is a select drop down list, I click on an item of that list, I trigger an Ajax request to controller/action and get back instantly from the server a value that I update a text area with.

4
  • 4
    JSON is no library. It is a data format like XML. Commented May 5, 2011 at 15:33
  • 1
    Just wondering if you could explain your aversion to using a library? They get written so that you don't have to rewrite 'classic' js like this over and over again. Commented May 5, 2011 at 15:35
  • About JSON: I want to make suer not using special Zend objects. Commented May 5, 2011 at 15:40
  • About not uisng libs: it's not an aversion, just a requirement for other related processing on the JavaScriot side. Commented May 5, 2011 at 15:41

1 Answer 1

1

The url you post to is all dependent on your controllers and modules structure, so that makes it hard for us to give you a detailed answer. But for a simple example, try this out.

form post to http://domain.com/index/textarea

the controller: IndexController.php

<?php

class IndexController extends Zend_Controller_Action {

    public function indexAction() {

    }

    public function textareaAction() {
        // if you are using layouts
        $this->_helper->layout->disableLayout();

        // process your post here
        $var = $this->_getParam('posted_var');

        $this->view->text =  '$var processed'; 
    }
}

the view: textarea.phtml

<?php

echo $this->text;

What ever is in your view, will then be returned to your JavaScript.

For a better implementation read up on AjaxContext switching, which will allow you to use JSON, which means you can drop the view file, and ZF will simply return a JSON encoded string for you.

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

1 Comment

Thanks. In fact I did it in the same pattern you mention. AjaxContext better not to use when you want to handle 100% by yourself; that's from experience. So layout disabling and JSON encoding must be done as per the documentation in the action method.

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.