2012-06-13 54 views
0

我想從zend控制器indexAction調用一個javascript函數。我的控制器看起來像這樣..如何從zend控制器indexAction調用JavaScript函數?

// mycontroller.php

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

和viwefile的控制器 //index.phtml

here is my javascript function 

    <script type='text/javascript'> 

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

回答

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> 
+0

謝謝..我可以調用控制器內的函數,如

+0

否。語法不正確(PHP變量需要'$'),並且您沒有變量'$ role'和'$ id'。我提出的代碼有什麼問題? – bububaba

相關問題