1

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!

3
  • does it helps var total_count = '<?php echo $tot_count;?>'; ? Commented Apr 2, 2014 at 21:05
  • 1
    var total_count = <?php $tot_count;?> should be var total_count = <?php echo $tot_count;?> Commented Apr 2, 2014 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/…> Commented Apr 2, 2014 at 21:13

5 Answers 5

2
<?php echo $tot_count;?>

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

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

3 Comments

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
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.
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?
1

There are many ways to do that.

1.You can directly request the variable from the server using ajax.This provides a better separation of layers.

2.You can bind the controller variable to the DOM element and get the value in javascript.

<input type="hidden" id="total_count" value="{{ total_count }}" />
var total_count = $("#total_count").val();

This operation is fast as you may not want to explicitly send a request for the variable each time.

3.Or directly echo in js variable

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

Although this method is simple,it tightly binds the presentation layer with the controller and is insecure.

Comments

0

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

1 Comment

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 :(
0

Try

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

instead of the suggested

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

1 Comment

If $tot_count is an integer, the quotes (') will convert it into a string.
0

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

An alternative you could consider is adding 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 then 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!

1 Comment

Be careful not to create additional elements just to hold the data -- this is avoidable DOM bloat.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.