0

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);
        }
    });
} 
5
  • is this URL dispatcher.php is right ? what is the error that you got ? Commented Feb 17, 2016 at 7:49
  • the url is right.. it gives an alert of error.. i don't know whether it going to that controller or not.. mohammad Commented Feb 17, 2016 at 7:52
  • Did you check what shown in Network section in browser developer tools? Maybe there is an error in your PHP code. Commented Feb 17, 2016 at 7:54
  • so your ajax can't be sent, that's because your URL is wrong Commented Feb 17, 2016 at 7:54
  • then please tell me how to send it from javascript n access in controller Commented Feb 17, 2016 at 7:56

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

18 Comments

thanks for ur ans bt still its nt working it give an error of [object object]
So instead of trying show error by alert(error) try console.log(error) and check what's in there.
@user3312547 this is another error, but now your ajax is sent successfully. check the way that you are using to collect the data from checkboxes.
is the method right which i have used data: {idList:idList}, to pass data of javascript to controller ??
i got this when i alert idlist array . [{"name":"autoId[]","value":"2840"},{"name":"autoId[]","value":"2829"},{"name":"autoId[]","value":"2836"},{"name":"autoId[]","value":"2835"}]. and i want to send all the autoid to controller through array @mohammad
|
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.