0

i want to call a javascript function from zend controller indexAction. my controller is look like this..

// mycontroller.php

      public function indexAction(){
         $role = 'admin';
         $id = 23;
      // here i want to call the javascript function 
       /// like myjsfun(role, id);
       }

and viwefile for the controller is //index.phtml

    here is my javascript function

   <script type='text/javascript'>

   function myjsfun(role, id){
    // code for this function
     }

1 Answers1

0

//mycontroller.php

$role = 'admin';
$id = 23;

$this->view->role = $role;
$this->view->id = $id;

//index.phtml

<script type='text/javascript'>

    function myjsfun(role, id){
        // code for this function
    }

    //actual call:
    myjsfun(<?php echo Zend_Json::encode($this->role) ?>, <?php echo Zend_Json::encode($this->id) ?>);
</script>
bububaba
  • 2,840
  • 6
  • 25
  • 29
  • thanks.. Can i call the function within controller like – Saeed Iqbal Jun 13 '12 at 13:32
  • No. The syntax is incorrect (PHP variables need a `$`) and you don't have a variable `$role` nor `$id` in scope. What is wrong with the code I proposed? – bububaba Jun 13 '12 at 22:12