3

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?

Mani Ratna
  • 883
  • 1
  • 11
  • 30
Ram lamsal
  • 87
  • 8

3 Answers3

1

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>
thepaks
  • 203
  • 1
  • 7
1

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

    ?>
0

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 } );
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • I have slightly update the answer.. But you obtain a var_dump(..) empty or no var_dump? – ScaisEdge Jan 03 '16 at 12:03
  • I tried your update and i could get value using var_dump but the problem is i have called another view in this controller .this view doesnot opens. – Ram lamsal Jan 05 '16 at 09:20
  • Explain better in $.post is called student/post and you which view ? action ? you wont .. please explain in a clear way so i can help you.. – ScaisEdge Jan 05 '16 at 16:50
  • I have updated the answer with the correct notation for actionPost. Then tell me if the var_dump show sometighs or not (updated your question with the resulto of var_dump) – ScaisEdge Jan 06 '16 at 07:49
  • i did that also i get value in $idListe too but now my problem is my view file _compose is not being called. url also doenot changes – Ram lamsal Jan 06 '16 at 08:16
  • i even tried your latest update but doesnot works.First when i am in studentReport my url appears like 'http://localhost/demo/index.php/student/studentReport ' and on click of button my url must appear something like 'http://localhost/demo/index.php/student/post '.This doesnot happens .When I enable firebug in my browser i could see my fom _compose code in console along with passed list. – Ram lamsal Jan 07 '16 at 05:01
  • Ok seems a Url problem . ( and the return is wrong don't use it..) i have update the answer.. – ScaisEdge Jan 07 '16 at 07:23
  • Should i use $.post( createAbsoluteUrl("post"); ?> { idList: selctedbox } ); instead – Ram lamsal Jan 07 '16 at 08:24
  • Yes i have update the answer .. i hope is more clear – ScaisEdge Jan 07 '16 at 08:30
  • I'm sorry.. evidently i don't understand your problem.. then my answer is unuseful – ScaisEdge Jan 07 '16 at 09:04