0

I am using the given script for auto-complete multiple values in a project. Here the code is working with the static values as given in the following script, but I want to make these values dynamic. When I try to use dynamic values in the same script, it doesn't work.

Here I am providing you the working code with static values.

  <script>
  $(function() {
    var availableTags = [
        "ActionScript",           // static values start
        "Ruby",
        "Scala",
        "Scheme"                  // static values end.
    ];
  });
  </script>

And here is the php code i tried to make dynamic the above script.

<?php 
    $recordd=mysql_query("select * from users ");
    while($row22=mysql_fetch_array($recordd)):
        $MessageObject[]='"' .$row22['fname']. '"';
    endwhile;
    $abc= implode(',<br>', $MessageObject);
?>

And now the script becomes as follow:

<script>
$(function() {
    var availableTags = [
    <?php echo $abc; ?>     // here is the code i made dynamic. which doesn't work.
    ];

});
</script>

How can I solve this?

6
  • can you share the Fiddle ? Commented Aug 19, 2014 at 11:35
  • Have you checked developers console? Are there any js errors? They should be as imploding with <br /> will cause js-error. Commented Aug 19, 2014 at 11:38
  • no, there is no error, but it gives null result. Commented Aug 19, 2014 at 11:39
  • Get rid of the <br> in this implode(',<br>', $MessageObject); as this is adding html to a javascript array definition. Think about what you would actually hard code between a javascript var availableTags = ['a','b','c']; and generate that in the PHP variable you are echo'ing into the js code. Commented Aug 19, 2014 at 11:41
  • but i need to apply this, just in the same format as used in static case, that's i used this <br> above, as now i have shortened the code, plz go through it once again, may be you would provide me a solution, thanks... Commented Aug 19, 2014 at 11:48

1 Answer 1

1

Without knowing what data is in your tables 'fname' column its a bit difficult to be definitive about an answer, but this would seem a simpler solution to your problem

<?php 
    $recordd=mysql_query("select * from users");
    $abc = '';
    while( $row22 = mysql_fetch_array($recordd) ):
        $abc .= '"' . $row22['fname'] . '",';
    endwhile;
    rtrim($abc, ',');
?>
Sign up to request clarification or add additional context in comments.

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.