I have a custom module, which uses for email varification in frontend
of magento. This module will appear as a popup in homepage while it loads. I want to do some other functionings such as display register form etc. in the same popup section, according to the output obtain from this email varification form…
Email varification happens in controller and it look like this
class Realuk_Login_IndexController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->loadLayout();
$this->renderLayout();
}
public function loginAction()
{
$session = Mage::getSingleton('customer/session');
//check whether session exist or not
if ($session->isLoggedIn())
{
return;
}
$result=array('error'=>'noError','success'=>false);
/*
email varification code comes here; according to this section
$array will set;
*/
//json encoding happens here
$jsonData=$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result)); /
$this->_redirect('home'); //this is my home page redirection
}
}
my frontend file that render popup section contains this code..
<?php
$array = Mage::helper('core')->jsonDecode($jsonData);
if(!empty($array))
{
echo "<pre>";
print_r($array);
echo "</pre>";
}
else
{
echo "no json data exists";
}
?>
It seems that $array
is empty every time in frontend. However in controller $jsonData
posses some values. My url look likes www.mydomain.com/home/
and no json value can see there
My question is..
1)Is this the proper way to get results from controller to frontend?
2)How to accomplish json decoding in frontend phtml file ?
Please share your thoughts.. It will help me and others also.
Thanks in advance
Best Answer
The proper way to send JSON data from the controller action is to do as such:
...where
$response
is an object or an array.However, the proper way to "consume" it or to use it depends on your application. The JSON 'decoding' is handled by your ajax method in the javascript. Here are a couple examples:
jQuery
Prototype.js
You can learn more about how to use Prototype's Ajax method in this screencast I produced: http://quick.as/7x6ska7 [end: shamelessplug]