1

I need to call a method of controller named test() from a Javascript. Script code is given below:

function changeColor(id, color) 
{
    element = document.getElementById(id);
    qnid = document.form1.pickupFrom.qnid;

    //window.location =  "<?= site_url('controller/test') ?>"
}

Is it possible? And how to pass the parameters?

1
  • What happened when you tried it? Is it your intention that the current page will be replaced by whatever that request returns? Commented Nov 14, 2011 at 7:11

2 Answers 2

1

It depends on what parameters youn need to pass. Are they PHP parameters? The you're doing just fine, or better see @Sudhir answer.

If your params come from javascript you shoulnd't use the site_url() function (since when it elaborates and spit out the url, javascript is yet to be executed). It would be much easier to build a URL this way:

function changeColor(id, color) {
            element = document.getElementById(id);
            qnid = document.form1.pickupFrom.qnid;

            window.location =  "<?php echo base_url();?>index.php/controller/test/"+element+"/"+qnid;
}

I'm assuming those two are the parameters you want to pass.

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

Comments

0

You should do it as:

window.location.href =  "<?php echo site_url('yourcontroller_name/function_name/param1/param2') ?>"

Hope it helps

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.