talking about...

PHP, Zend Framework, jQuery, Javascript, MySQL and other web development topics.

jQuery AJAX in Zend Framework

Filed Under (General) by Wenbert on 28-08-2007

Tagged Under : , ,

I have posted a sample code on my wiki on how to submit variables to an action controller. The sample code contains a niffty jQuery plugin - the jQuery Calendar.

Click here for the tutorial.

Update (07-08-2008): For those who don’t know, Zend Framework has built-in stuff for Dojo. So you might want to check it out first. I still prefer jQuery though.

Update (05-07-2008): Since this post has been getting a lot of traffic and the tutorial to my wiki is not that good, I have decided to update this page and post a simpler and shorter tutorial.

So here it is…

The Javascript will look something like this:

//For Edit Action
f u n c t i o n editnote(notes_id)
{
//a sample loading image thingy
$('#notes_entry_id').prepend("
<div id="loading"><img src="http://blog.ekini.net/wp-admin/&quot;+baseUrl+&quot;/images/loading.gif" alt="" /></div>
");
//Get notes from database - in ZF, notes = controller and getnoteforedit = action
$.post(baseUrl+"/notes/getnoteforedit",{
notes_id: notes_id
}, function(data){
$('#loading').remove();
obj = window.eval(data);
$('#textarea_id').val(obj['notes']);
}, "json"); //this returns JSON.
}

Then my controller would look something like this:

    /**
* Get note for action
*
*/
public function getnoteforeditAction()
{
$this-&gt;_helper-&gt;layout-&gt;disableLayout();    //disable layout
$this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(); //suppress auto-rendering
//the 2 lines above are very important. this action would return html tags from the layout and will look for a phtml file. we disable the layout and suppress auto-rendering of the phtml view files SO that our JSON will be echoed properly to the Javascript...
require_once('models/Notes.php');
require_once('models/UsersNotes.php');
try {
if (!$this-&gt;_request-&gt;isPost()) {
throw new Exception('Invalid action. Not post.');
}
$Notes = new Notes();
$data = array();
if ($result = $Notes-&gt;fetchRow("notes_id=".$Notes-&gt;getAdapter()-&gt;quote($_POST['notes_id'])."")-&gt;toArray()) {
$data['notes_id']             = $result["notes_id"];
$data['notes']                = $result["notes"];
$data['datetime_posted'] = $result["datetime_posted"];
$data['status']               = $result["status"];
$json = Zend_Json::encode($data);    //basically, $data array will also be available in the JS.
} else {
throw new Exception('Note ID not found.');
}
echo $json; //this will echo JSON to the Javascript
unset($json);
unset($data);
} catch (Exception $e) {
echo $e-&gt;getMessage();
}
}

That’s about it… :D
The $data that was encoded using Zend_JSON in the controller/action can now be accessed in the Javascript after you call the eval().

Related posts:

  1. How to do AJAX in jQuery with JSON callback in Zend Framework Here is how to do it in Zend Framework. Let’s...
  2. Mixing PHP Variables with Javascript (Zend Framework and jQuery) Sometimes, I “mix” PHP variables with Javascript. For example, I...
  3. jQuery: Using AutoComplete on dynamically created elements This is a common problem wherein you dynamically create your...
  4. Zend Framework How To: Handling checkboxes using Zend_Form and jQuery I am sharing this because it took me a while...
  5. Zend Framework Zend_Auth and a Plugin to Check Access to a Page I have written this simple user authentication and user access...

Related posts brought to you by Yet Another Related Posts Plugin.

Comments:

3 Responses to “jQuery AJAX in Zend Framework”


  1. I use jQuery-PHP library with ZF, see example on project homepage


  2. Thanks Anton. Have you created a proposal for this in ZF? It would be great if this made it to the official ZF release.


  3. couldnt u you do this in your controller? it works for me:

    $this->_helper->json(Zend_Json::encode($array));

Leave a Reply

Subscribe to Rss Feed : Rss

Bad Behavior has blocked 619 access attempts in the last 7 days.