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 am implementing tags feature in my project. In my demo tags i found they are passing names in a javascript function for the autocomple.

This is a function in my demo project,

   <script>
   $(function()
      {
          var sampleTags = ['c++', 'scala', 'groovy', 'haskell', 'perl', 'erlang', 'apl', 'cobol', 'go', 'lua'];
      ..................
      .................

So , i want pass values from my php controller to this function ,inorder to get the autocomplete value from my database table

For example i am getting tags values from my db in my Controller like this:

 ` $data["query"] = $this->ordermodel->fetch_orderlist();`
   $this->load->view('tagpage', $data);  //loading my page tag page where above function exists

Now how can i pass that $data["query"] values into the above javascript function? Please help

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You could echo the variable onto the page using PHP's json_encode. This function will convert a PHP array or object into a JSON object, which JavaScript can work with:

<script>
$(function() {
      var sampleTags = <?php echo json_encode($query); ?>;
})();
</script>

But a better way would be to request these values via Ajax. Say you have a PHP script named values.php:

<?php
    #...
    #assign $query here
    #...
    echo json_encode($query);

Then, in JavaScript (on the page where you want to use the sampleTags variable), you can use jQuery's .ajax function to make an easy Ajax request:

<script>
var sampleTags;

$.ajax({
    url: 'values.php'
}).done(function(data) {
    if (data) {
       sampleTags = data;
    }
});
</script>

I haven't tested this example. Obviously you'll want to tweak it to fit your environment.

share|improve this answer
1  
I believe it would just be $query and not $data['query'] –  Dale May 8 '13 at 11:12
    
i think you mean $data, right? either way it's the same concept. –  sgroves May 8 '13 at 11:14
    
No I mean $query :) Codeigniter will separate out all the indexes of the array passed in to a view, the concept is the same yes but broken code is broken code. –  Dale May 8 '13 at 11:17
    
ah, okay. i've never used codeigniter. thanks. –  sgroves May 8 '13 at 11:17
    
@sgroves I was trying your first method for getting the list but its not working. Its not showing the list... i used this var sampleTags = <?php echo json_encode($data); ?>; in my view & my contrller & model ,those i update above in my question –  Donkey May 8 '13 at 11:39

You will have to write an ajax function for this

$.ajax(
    url     :   "<?php echo site_url('controller/method')?>",
    type    :   'POST',
    data    :   'para=1',
    success :   function(data)
    {
        if(data){
            var sampleTags  =   data;
        }
    }
);
share|improve this answer
    
well, you don't HAVE to. but ajax is definitely the way to go. –  sgroves May 8 '13 at 11:16
    
@raheel after seeing your answer , i tried your ajax function, but its not showing the output. I updated my model and controller in my question. Please check and let me know your suggestion.. –  Donkey May 8 '13 at 12:58
    
@Ashutosh i only presented you an example you can replace with your paramters. change controller and method to your controller name and method name –  raheel shan May 8 '13 at 13:06
    
@raheelshan Obviously i changed those stuffs. but why its not showing the output? –  Donkey May 8 '13 at 13:09
    
@Ashutosh hit the controller method directly in browser to see if the output is coming. Also use console.log(data) to see if there comes anything in data –  raheel shan May 8 '13 at 13:37

Just echo your php variable

 $(function()
      {
          var sampleTags = '<?php echo $data["query"]; ?>'
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.