2

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

1
  • either make a HTML form with data in hidden fields & then submit it using JavaScript or use AJAX here. Commented May 4, 2016 at 5:47

3 Answers 3

1

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;

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

2 Comments

I probably made a mistake somewhere, because it doesn't do anything, I will try again and I will tell you did I succeed. Thank you for the answer!!!
Thank you for the answer, I made a mistake and fix it, and now your code works fine!
0

try this

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

Comments

0

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;

1 Comment

I probably made a mistake somewhere, because it doesn't do anything, I will try again and I will tell you did I succeed. Thank you for the answer!!!

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.