YII:如何将参数列表从js传递到控制器,并通过传递另一个列表来调用新视图

YII: How to pass list of parameters from js to controller and call new view by passing another list?

本文关键字:列表 另一个 新视图 调用 控制器 参数 js YII      更新时间:2023-09-26

我是yii的新手。我试图在"点击"按钮的情况下将参数列表从js传递到我的控制器
我的代码是(在名为"studentReport"的视图中):

echo CHtml::submitButton('post' ,array('onclick'=>'js:passParams()',  'type' => 'POST',
,'name' => 'approveBtn'));

我的js代码在同一个表单中:

function passParams(){
  var selctedbox = [];
  for(var i=0; i<fields.length; i++){
    selctedbox .push(fields[i].value);
  }
  $.post( <?php echo "'" . $this->createUrl('student/post') ."'" ; ?>, 
   { idList: selctedbox } );
}

我的控制器代码是:

public function actionPost()
{
  $newList= array();
  $idListe=$_POST;
  foreach ($idListe['idList'] as $value) {
    $newList[]=$value;
  }
  $this->render('_compose',array('newList'=>$newList,'model'=>$model));
}

在这里,我想将我的值列表传递给action actionPost()
我不想在url中显示传递的参数,也不想用控制器传递的参数打开一个新的视图
我该怎么做?

尝试以下代码:

<?php echo CHtml::Button('SUBMIT',array('onclick'=>'passParams();')); ?> 
<script type="text/javascript">
    function passParams(){ 
        var data=$("#formID").serialize();
        $.ajax({
            type: 'POST',
            url: '<?php echo Yii::app()->createAbsoluteUrl("student/post"); ?>',
            data:data,
            success:function(data){
                alert(data); 
            },
            error: function(data) { // if error occured
                alert("Error occured.please try again");
                alert(data);
            }
        });
    } 
</script>

您也可以使用ajaxsubmit按钮。

 <?php
        echo CHtml::ajaxSubmitButton ('Create','default/create',
                    array(
                        'data'=>'js:$("#test-form").serialize()',
                        'method'=>'post'  ,
                         'success' => 'function(html) {  
                                    if(html=="success"){ 
                                        window.location="";
                                    }else{
                                        jQuery("#errorSummary").html(html)
                                    }}',
            ),array('class'=>'test') ); 
    ?>

使用ajax Jquery Post

 function passParams(){
 var selectedbox = [];
 selectedbox.push(fields[1,2,3]);
$.post( <?php echo "'" . $this->createUrl('student/post') ."'" ; ?>, 
       { try: 'atry', postselectedbox: selectedbox } );    
}

学生/后

var_dump($_POST)  //so you can check the content

记住记法是

public function actionPost()    // camelCase  and not actionpost
{
   // and for check add 
   var_dump($_POST)
   $newList= array();
   $idListe=$_POST;
   .......

 this->render('_compose',array('newList'=>$newList,'model'=>$model));

如果你的url是错误的(学生/学生)删除学生表单url

$.post( <?php echo "'" . $this->createUrl('post') ."'" ; ?>, 
 { idList: selctedbox } );
相关文章: