0

Hi here i am using the jquery auto-complete plugin. i have some php retrieved database values i want to use those values in the autocomplete plugin. for that i want to get the php values to javascript array. how can i do this?

$(function() {
    var availableTags = [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
    ];
    $( "#category" ).autocomplete({
      source: availableTags
    });
  });

Php:

<?php
require_once "config.php";
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select distinct(category) from completer";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $fname = $rs['category'];
    echo "$fname"
}
?>
5
  • 1
    Why don't you use json_encode($your_array); Commented Sep 21, 2015 at 7:21
  • JS and PHP are in same file or you're using ajax ? Commented Sep 21, 2015 at 7:22
  • @Fky they are not in same file. Commented Sep 21, 2015 at 7:23
  • Use Ajax request to get your php content , and in your php use json_encode() function to transform PHP to Json string expected by JS. See documentation : php.net/manual/en/function.json-encode.php Commented Sep 21, 2015 at 7:25
  • No need to create ajax request. If the data is prepared while at the time of page load. Like the js in some js file, and some body.php you can echo a script tag, and create a global variable and inside the js, you can use the php echo to populate a javascript variable. Only ajax is needed (in this case) when the data depends on some user interaction (like selecting a value and on the basis of that the javascript value should get populated from the database) Commented Sep 21, 2015 at 7:29

3 Answers 3

2

Well, if you have the data store inside php variable, then you can add those values into javascript variable like so, no need for ajax stuff for this :

Supposed you have values inside php variable like :

$myPhpVar = [
  "ActionScript",
  "AppleScript",
  "Asp",
  "BASIC",
  "C",
  "C++",
  "Clojure",
  "COBOL",
  "ColdFusion",
  "Erlang",
  "Fortran",
];

Then in your js part should be :

$(function() {
  var availableTags = [];

  // start here - populate the data from php variable
  <?php foreach($myPhpVar as $key => $val) {?>
    availableTags.push('<?php echo $val;?>'); // push data into js variable
  <?php }?>
  // end here

  $( "#category" ).autocomplete({
    source: availableTags
  });
});
1

JS script :

$(function() {
    $.ajax({
        type : 'get',
        url : 'urlofmyfile.php',
        data : 'q='+q,
        dataType : 'json',
        success : function(availableTags){
            $( "#category" ).autocomplete({
              source: availableTags
            });
        }
    });

  });

$.ajax documentation : http://api.jquery.com/jquery.ajax/

PHP script :

  <?php
require_once "config.php";
$q = strtolower($_GET["q"]);
if (!$q) return;
$sql = "select distinct(category) from completer";
$rsd = mysql_query($sql);
while($rs = mysql_fetch_array($rsd)) {
    $fname[] = $rs['category'];
}
print json_encode($fname);
exit;
?>

json_encode documentation : http://php.net/manual/en/function.json-encode.php

1

There are several ways to achieve this.

Dynamic page generation.

You can dynamically get values from database and insert into the HTML as JSON object:

$(function() {
    var availableTags = <?php
        require_once "config.php";
        $q = strtolower($_GET["q"]);
        if (!$q) die("[]");
        $sql = "select distinct(category) from completer";
        $rsd = mysql_query($sql);
        $row = array();
        while($rs = mysql_fetch_assoc($rsd)) {
            $row[] = $rs['category'];
        }
        echo json_encode($row);
    ?>;
    $("#category").autocomplete({
        source: availableTags
    });
});

AJAX.

AJAX approach is almost the same but it is considered to be a better practice. In this case, PHP file is a separate file which returns only an object handled by JS.

PHP:

<?php
    require_once "config.php";
    $q = strtolower($_GET["q"]);
    if (!$q) die("[]");
    $sql = "select distinct(category) from completer";
    $rsd = mysql_query($sql);
    $row = array();
    while($rs = mysql_fetch_assoc($rsd)) {
        $row[] = $rs['category'];
    }
    echo json_encode($row);
?>

JS:

$(function() {
    $.ajax({
        url: 'yourPhp.php',
        data: 'q=' + q,
        dataType: 'json'
    }).done(function(dt) {     
        $("#category").autocomplete({
            source: dt
        });
    });
});

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.