Yii,从 Javascript 函数调用控制器函数
Yii, calling controller function from Javascript function
我有一个 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
。
你可以试试这个
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+'...;
我有一个 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
。
你可以试试这个
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+'...;