Ram lamsal
Ram lamsal

Reputation: 87

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

I am new to yii. I am trying to pass a list of parameters from js to my controller in the event of 'on click' of a button.
My code is (in the view named 'studentReport'):

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

My js code inside the same form:

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 } );
}

My controller code is:

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

}

Here I want to pass my list of values to the action actionPost().
I don't want to show passed parameters in url and open a new view with passed parameters from controller.
How should I do it?

Upvotes: 3

Views: 1901

Answers (3)

ScaisEdge
ScaisEdge

Reputation: 133360

Use ajax Jquery Post

 function passParams(){
 var selectedbox = [];
 selectedbox.push(fields[1,2,3]);

$.post( <?php echo "'" . $this->createUrl('student/post') ."'" ; ?>, 
       { try: 'atry', postselectedbox: selectedbox } );    
}

in student/post

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

and remender the notation is

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

If your url is wrong (student/student) remove student form url

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

Upvotes: 0

Anamika Shrivastava
Anamika Shrivastava

Reputation: 713

You can use the ajaxsubmit button also.

 <?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') ); 

    ?>

Upvotes: 1

thepaks
thepaks

Reputation: 203

Try below code:

<?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>

Upvotes: 1

Related Questions