Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been unable to pass variable from code igniter controller to a javascript variable

I have a controller that runs a model to return a number

$tot_count = $this->model_users->tot_count();

Than in the header of the view I'm trying to get the value of $tot_count. I have tried several things but I'm unable to get the value passed as

$(document).ready(function(){
    var total_count = <?php $tot_count;?>

        alert("Value: " + total_count);
    });

But get nothing, please help!

share|improve this question
    
does it helps var total_count = '<?php echo $tot_count;?>'; ? –  Abhik Chakraborty Apr 2 at 21:05
1  
var total_count = <?php $tot_count;?> should be var total_count = <?php echo $tot_count;?> –  cale_b Apr 2 at 21:05
    
I have tried the echo also it does not work var total_count = <?php echo $tot_count;?>; alert("Value: " + total_count); Also I have tried var total_count = '<?php echo $tot_count;?>'; but it does not work. Is it because I'm using <script src="ajax.googleapis.com/ajax/libs/jquery/1.11.0/…; –  user3491261 Apr 2 at 21:13

4 Answers 4

<?php echo $tot_count;?>

you are not echoing anything. you need to use echo or any directive that send something to the output.

share|improve this answer
    
I have tried the echo also it does not work, system gives no alert message when I use the echo, if I do not use the echo at least I get a popup alert but with no value passed –  user3491261 Apr 2 at 21:16
    
it doesnt work because you must have an error somewhere,try to echo a number and instead of a variable and see if it works. –  mpm Apr 2 at 21:22
    
yes that works, but not passing of variable value from controller to the javascript variable. these syntax cannot get the value. I use <script src="ajax.googleapis.com/ajax/libs/jquery/1.11.0/…; is that any problem? –  user3491261 Apr 2 at 21:34

Might be a stupid suggestion but try to put a semicolon after the variable declaration:

var total_count = <?php $tot_count;?>;

If you're not closing it, then probably the alert is assigned to the variable..

share|improve this answer
    
Sorry, I have tried this is not working also, I'm seeing that you need to single or double quote to get the alert to work, else no alert popup is also displayed. var total_count = '<?php $tot_count;?>'; When I echo the value from the controller it can be clearly seen a number like 22 assigned to it, but it just cannot pass to the Javascript variable :( –  user3491261 Apr 2 at 21:32

Try

var total_count = '<?php echo $tot_count;?>';

instead of the suggested

var total_count = <?php $tot_count;?>;
share|improve this answer
    
If $tot_count is an integer, the quotes (') will convert it into a string. –  Jeremy J Starcher Apr 3 at 15:04
up vote 0 down vote accepted

I have solved this issue, hope this is useful to someone in the future.

An alternative you could consider is to add the variable needed by the Javascript to some sort of HTML element in your view, and then use JS to pull that variable and then do whatever you need with it.

From the controller echo the value within a div id = 'demo' and than from Javscript call.

var total_count = 0; total_count = document.getElementById("demo").innerHTML; document.getElementById("demo").innerHTML = ''; // this is to not display the value but to capture into a Javscript variable and use it and not display from echoed value from controller.

That's it!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.