Yii,从Javascript函数调用控制器函数

时间:2015-04-28 07:10:05

标签: javascript php yii

我有一个html按钮,我想通过控制器动作生成一个报告,例如,

在我的PaymentController.php

public function actionGenerateRI($date){...}

在我的admin.php中

<button type="button" onclick="generateRI()">Generate</button>

<script>
function generateRI(){
    var date = document.getElementById("month").value +"-"+ document.getElementById("year").value;
    //what should I write here to call the actionGenerateRI? 
}
</script>

如何使用我的按钮调用控制器功能并传递变量date

P / S:它们都属于同一模型,在本例中为Payment

1 个答案:

答案 0 :(得分:1)

你可以试试这个

var date = document.getElementById("month").value +"-"+ document.getElementById("year").value;

// If you are using path format
window.location.href= '<?php echo Yii::app()->createUrl('payment/generateRI'); ?>' + '/date/'+encodeURI(date);

// otherwise
window.location.href= '<?php echo Yii::app()->createUrl('payment/generateRI'); ?>' + '&date='+encodeURI(date);

// For more variables
window.location.href= '<?php echo Yii::app()->createUrl('payment/generateRI'); ?>' + '&date='+encodeURI(date)+'&variable2='+var2_value+'&variable3='+var3_value+'...;