user3312547
user3312547

Reputation: 41

how to send javascript array to controller in yii

I have a view having multiple checkboxes.

When I select some textbox, I need to do some operation on them, so they should be passed to the controller.

How can I do this?


My JS function :

function myfun() {
    $.ajax({
        type : "POST",
        url : "http://localhost/newtemplate/index.php/product/dispatchdata/",
        dataType : 'json',
        data : { 
            idList : $("input[type=checkbox]:checked").serializeArray()
        },
        success :  function(data) {
            alert(data);
        },
        error : function (data){
            alert('Error');
            //alert(data);
        }
    });
} 

Upvotes: 0

Views: 504

Answers (3)

rushil
rushil

Reputation: 576

As mohammad said: "Replace your url with"

url: "<?php echo Yii::app()->createUrl('product/dispatchdata'); ?>",

Further, if you fail in ajax call, the reason may be that the error is in your code in the controller. if you cant return successfully from controller, then you will get error in ajax... so try debugging step by step your controller action.

Upvotes: 0

RAJESH CHAUHAN
RAJESH CHAUHAN

Reputation: 89

Use this :

$.ajax({
    url: "<?php echo App::param('siteurl'); ?>products/ZipUpload",
    type: "POST",
    data: {
        idList : $("input[type=checkbox]:checked").serializeArray()
    },
    success: function(data) {
        alert(data)
    }
});

Upvotes: 0

Mohammad
Mohammad

Reputation: 3547

replace your URL by this:

url: "<?php echo Yii::app()->createUrl('product/dispatchdata'); ?>",

then try to send your json as follow: data: {idList:JSON.stringify(idList)},

Upvotes: 1

Related Questions