1

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

3 Answers 3

2

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.

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

11 Comments

I believe it would just be $query and not $data['query']
i think you mean $data, right? either way it's the same concept.
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.
@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
@sgroves Even i tried the second way also, with ajax request. This is also not working. Now showing data...Please help...
|
0

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;
        }
    }
);

6 Comments

well, you don't HAVE to. but ajax is definitely the way to go.
@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..
@Ashutosh i only presented you an example you can replace with your paramters. change controller and method to your controller name and method name
@raheelshan Obviously i changed those stuffs. but why its not showing the output?
@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
|
0

Just echo your php variable

 $(function()
      {
          var sampleTags = '<?php echo $data["query"]; ?>'

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.