I have the following array:

$given_themes = array("ActionScript", "AppleScript", "Asp", "BASIC", "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang", "Fortran", "Groovy", "Haskell", "Java", "JavaScript", "Lisp", "Perl", "PHP", "Python", "Ruby", "Scala", "Scheme");

I want to use these items in the following autocomplete script, but it is not working... How do I get it to recognize the array values? I am sure it has something to do with how I call the variable $given_themes...

<script>
$(function() {
var availableTags = [ <?php $given_themes; ?>];
	$( "#tags" ).autocomplete({
	source: availableTags
	});
});
</script>

<input name="theme" type="text" id="tags" value="<?php print "$theme"; ?>"/>

    I believe you want to change this line to as shown:

    var availableTags = [ <?php implode(',',$given_themes); ?>];

      While Derokorian is probably correct in suggesting that a function such as [man]implode/man should be used (since you can't simply print the contents of an array variable directly), you're probably also missing a [man]print[/man] or [man]echo[/man] statement.

      Otherwise, you've got code that essentially is the same as :

      <?php
      "Hello world!";
      ?>

      which is the same as:

      <?php
      ?>

      .

        Wow didn't even notice it didn't have an output function.... lol I failed.

          Thanks, yeah I just noticed the no "echo" too.... but it still isn't working. I am not getting anything when I type in the input box. No autocomplete whatsoever... Here's what I did per your suggestions:

          <script>
          $(function() {
          var availableTags = [ 
          	<?php implode(',',$given_themes); ?>
          ];
              $( "#tags" ).autocomplete({
          	source: availableTags
              });
          });
          </script>
          

          And I also tried:

          <script>
          $(function() {
          var availableTags = [ 
          	<?php echo implode(',',$given_themes); ?>
          ];
              $( "#tags" ).autocomplete({
          	source: availableTags
              });
          });
          </script>
          

            Nevermind... figured it out... thanks.

            It was silly... I had put the array under if (isset ($_POST[]) in the php... I moved the array to the top of the page, and it works.

              Hello, I just wanted to reply to this post with my problem since its pretty much the same. I have tried using the solution proposed but i am unable to get any results.
              *getUserNames() is a function that returns a string array.

              <?php $screenName = getUserNames()?>

              <script>
              $(document).ready(function() {
              var userNames = [ <?php echo implode(',',$screenName)?>];
              $("#t_update").autocomplete({
              source: userNames
              });

              });
              </script>

              Even if i hardcode the array for the $screenName i am still unable to get the autocomplete function working. Pls help. I am still a newbie in php so i am still figuring things out. Thanks 🙂

                Write a Reply...