Ajax Example in Zend Framework


/* Part1: Client Side Code  */

<div id="content">
    <a href="javascript:void(0);" onclick="callAjax();">Click Here To Call Ajax Method</a>  
</div>

<script>
function callAjax()
{
    var baseurl = getBaseURL();
    var data = '';
    var param1 = '1';
    var param2 = '2';
    var param3 = '3';
    data += "param1="+param1+"&param2="+param2+"&param3="+param3;
    jQuery.ajax({
        url: baseurl+"controllerName/actionName",
        type: "POST",
        dataType: 'json',
        data: data,
        success: function(data){
            for(i=0;i<data.length;i++){
               alert(data[i]);
           }
        },
        error:function(){
            alert("Ajax request failed.");
        }
    });
}

</script>

/* Part2: Server Side Code  */


/* Contrller Name: Index
   Action Name: testajaxcall
*/
class IndexController extends Zend_Controller_Action {
public function testajaxcallAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$params = $this->_request->getParams();

if (isset($params['param1']))
$param1 = $params['param1'];

if ("" != $param1)
{
$msg = array();
/* Business logic here
---- --- --
---
*/
$msg[] = 'Success';
echo json_encode($msg);
exit;
}
}
}

Note: You will know more about jQuery & Download latest version of jQuery from  
http://jquery.com/
Ajax Example in Zend Framework Ajax Example in Zend Framework Reviewed by Pranay Gondane on 4:14:00 AM Rating: 5

No comments

Free! Free!Free! Subscribe to Get Free PHP (Magento) tutorial Update in Your Inbox!!! Hurry Up!!!