簡體   English   中英

YII:如何將參數列表從js傳遞給控制器​​並通過傳遞另一個列表來調用新視圖?

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

我是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));

如果您的網址錯誤(學生/學生),請刪除學生表單網址

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM