useruser
useruser

Reputation: 159

calling codeigniter method from javascript and passing data

Hello I am using codeigniter framework for my college project ,and I need to call controllers method from js function, and i manage to do that, but now I dont know how to pass data to that method that I am calling. I was found some examples on web but I didn't manage to solve my problem.

This is how I call my method from js function,

window.location.href = "<?php echo site_url('controller_user/test');?>";

and I need to send this data to that method

var data = [formName, formSurname, formEmail, formUsername];

And I try something like this:

window.location.href = "<?php echo site_url('controller_user/test');?>?data="+data;

I don't know what to do to solve this problem, like I said I found something similar on stackoverflow site but still having trouble.

Thanks to everyone willing to help me!!!

Upvotes: 2

Views: 1574

Answers (3)

krishnakumar kk
krishnakumar kk

Reputation: 289

please modify your JavaScript like the following

window.location.href = "<?php echo site_url('controller_user/test');?>/"+formName+/+formSurname+'/'+formEmail+'/'+formUsername;

modify your controller function like the following

public function test($formName='',$formSurname='',$formEmail='',formUsername=''){

    echo "$formName ,$formSurname,$formEmail,formUsername";
    exit;

}

Upvotes: 1

Amit Chauhan
Amit Chauhan

Reputation: 682

you should use ajax and passed the data to method . like this , try out

var data = {formName : formName , formSurname : formSurname ,formEmail : formEmail,formUsername : formUsername}

     $.ajax({
            url: '<?php echo base_url('controller_user/test');?>',
            type: 'POST',
            data: data,
            success: function(msg)
            {
                //success part code 
            }
        });
        return false;

Upvotes: 0

Rafael Mota
Rafael Mota

Reputation: 127

try this

window.location.href = "<?php echo json_encode(site_url('controller_user/test'));?>

Upvotes: 0

Related Questions