by
faheem on July 27th, 2009
I have already written about json in one, actually some of my articles. Cool functionality can be achieved using json. Thanks to http://www.json.org, http://www.zend.com and http://www.dojotoolkit.org for providing easier solutions.
I’m not gonna talk what these three are all about, but instead share a little secret about these.
Json creation in dojo is as simple as this.
var arr = new Array();
arr[0] = "apple";
arr[1] = "ball";
arr[2] = "cat";
dojo.toJson(arr);
Using Zend to convert array into json took only one line of code.
Zend_Json::encode($arr);
And decoding- converting json to array, is as simple as this
Zend_Json::decode(json_string);
by
faheem on July 13th, 2009
While working with AJAX, you will need to get data after sending request to the server. You can use three ways to get the data form the server.
1. In xml Format
2. HTML Format
3. Json Format
Though xml has several advantages, however processing data at the client will put extra burden.
HTML, can easily be handle, is however more resource or you can say bandwidth extensive.
So JSON fall in the middle of both.
PHP provide a very easy way to create JSON response. Once you get data at client side you will need much later code to process response than xml document.
Let’s first look at the server side.
For example we have the following array.
$data = array(
'items'=>array(
'firstname'=>'faheem',
'lastname'=>'abbas',
'address'=>'pakistan'
)
);
To create JSON response, simply write
echo json_encode($data);
This will give the following result
{"items":{"firstname":"faheem","lastname":"abbas","address":"pakistan"}}
Now at the client side to send AJAX request, you will need to create the following code.
Keep in mind that we are using prototype for making AJAX request.
That’s it.
by
faheem on July 12th, 2009
Well, while working with ajax, it better to have an xml as response. Xml, however need more coding and parsing on client side than json.
Zend framework provides very easy way to generate json response.
In this article I’m going to discuss how to create Ajax request using prototype and how to use Zend_Json to generate response.
For generating Ajax request, you will need to write
If response is the form of this
[
{"firstName":"Faheem","lastName":"Abbas","username":"fahim","email":"faheem.abbas@example.com"},
{"firstName":"Shazil","lastName":"Hassan","username":"shazil","email":"shazil.hassan@yahoo.com"}
]
Then to acess the values of the first row, write
var firstName = results[0].firstName;
var lastName = results[0].lastName;
and the values of the second row can be accessed as
var firstName = results[1]. firstName;
var lastName = results[1].lastName;
To create Json response, you will need to write the following in your controller action
$users = new Users();
$username = $this->_getParam('username');
$select = $users->select();
$data = $users->fetchAll($select);
$results = $data->toArray();
echo Zend_Json::encode($results);
Keep in mind that “users” is our model, having the following code.
class Users extends Zend_Db_Table
{
protected $_name = "users";
}
That’s it. cheers.
by
faheem on July 6th, 2009
I used JSon for the first time in my application today. Zend provide a very easy way to create JSon response.
In this article I would use Prototype to make ajax call. Hopefully you will now a bit about Prototype. Anyway if not, it would not be a hard job to understand it after reading my article.
So let’s get started.
Consider you have the following form.

The scenario is
When user select name from the dropdown, the entire form is filled with the data from the database for that particular user.
For this purpose you would need to have a controller/Action(s), a model and template file.
In your form attach the following attrib to your name element as
$name = $this->createElement('select','name');
$name->addMultioptions(array(
'select'=>'[select]',
'1' => 'Faheem',
'2' => 'Abbas'
));
$name->setAttrib('onchange','AutoFill()');
In the lines above we first create a dropdown give it two values. And then attach javascript function “AutoFill” function using setAttrib method.
In your controller initialize your form and assign it to the view template as
$form = new MyCustomForm();
$this->view->form = $form;
Now in your view template file write the following.
Write the following javascript code.
To get response you will need to create an action and write the following code
class User extends Zend_Controller_Action
{
public function indexAction()
{
$form = new MyCustomForm();
$this->view->form = $form;
}
public function getdataAction()
{
$this->_helper->layout()->disableLayout();
$users = new Users();
$this->_helper->viewRenderer->setNoRender();
if ($this->getRequest()->isXmlHttpRequest()) {
$id = $this->_getParam('id');
$userData = $ users ->getData($id);
$dojoData= new Zend_Dojo_Data('id',$userData,'id');
echo $dojoData->toJson();
}
}
}
In the above code getdataAction is called using ajax call. In the first line we have disabled the layout, because we don’t need layout in case of ajax response. Next we create object of our model class(discussed later). Next we tell zend to disable view rendering.
In the next few lines we check for ajax request, get id, call our getData model method.
We then create dojo data object. This object provide a very useful method to convert data retrieved into json format. The method used is toJson().
In our model we have code like the following.
class Users extends Zend_Db_Table
{
protected $_name = 'users';
public function getData($id)
{
$select = $this->_db->select()
->from($this->_name,
array('address1','address2'….))
->where('id = ?', $id);
$result = $this->getAdapter()->fetchAll($select);
return $result;
}
}
That’s it. Cheers.