Get MySQL data with PHP and go to JavaScript

1

I'm trying to use jquery to do an auto complete in my search field. The connection to the bank is correct and I use the following code to save the files:

<?php

INCLUDE "conexao.php";



$sql = "SELECT * FROM consulta";
$result = mysqli_query($conexao, $sql);
$potential = mysql_fetch_array($result);
echo json_encode($potential);

?>

Using this javascript code, auto complete works:

  <script>
$(function() {
var availableTags = [

  "Felipe Arcaro",
  "Amanda Bertollini",
  "Rafael Manaus",
  "Gleidson",
  "Guilherme Sato",
  "C++",
  "Clojure",
  "COBOL",
  "ColdFusion",
  "Erlang",
  "Fortran",
  "Groovy",
  "Haskell",
  "Java",
  "JavaScript",
  "Lisp",
  "Perl",
  "PHP",
  "Python",
  "Ruby",
  "Scala",
  "Scheme"
];
$( "#tags" ).autocomplete({
  source: availableTags
});
});
</script>

But using this, no:

<script>
$(document).ready(function(){
var availableTags = <?php print(json_encode($potential)); ?>;
console.log(myArray);
$( "#tags" ).autocomplete({
  source: availableTags
});
});
</script>
    
asked by anonymous 26.09.2015 / 21:58

2 answers

1

In case you need to send a request via ajax and to list, you have to define the parameter that will be listed, in case I put a tag field:

<?php

require "conexao.php";
$sql = "SELECT tag FROM consulta";
$result = mysqli_query($conexao, $sql);
$potential = mysql_fetch_array($result);
echo json_encode(array('result' => $potential));
die();
?> 

I imagine your converted output is something like this:

{"result":
    {
         "0":"PHP",
         "1":"Javascript",
         "2":"Ruby"
    }
}

So for your script to work, you should get the parameter through a request to your file: your_ajax.php

<script>
$(function() {
     ('#tags').autocomplete({
         source: function (request, response) {
             $.post("seu_ajax.php", function (data) {
                 response($.map(data.result, function (value, key) {
                     return {
                         label: value,
                         value: key
                     };
                 }));
             });
    },
    minLength: 2,
    delay: 100
});
</script>
    
28.09.2015 / 20:01
0

I made it work.

Complete Code.php:

<?
include "conexao.php";

$search = mysqli_real_escape_string($_GET['term']);
$sql = "SELECT * FROM consulta";
$result = mysqli_query($conexao, $sql) or die(mysqli_error());

$resJsonF = '[';
$first = true;

while($res = mysqli_fetch_assoc($result)):

    if(!$first):
    $resJsonF .= ', ';

    else:
     $first = false;
    endif;
    $resJsonF .= json_encode($res['funcao']);

endwhile;

$resJson .= ']';



?>

Do not forget to call the complet.php function at the beginning of the code!

JavaScript code:

<script>
$(function() {
var tagNome = <?php echo $resJsonN; ?>;
$( "#completaNome" ).autocomplete({
source: tagNome
});
});

How do I create a condition to bring only the 4 most pertinent in self completion? I want this because I will be working with a database of 30k lines, and I believe this is necessary. Does jquery autocomplete already have this default condition?

    
27.09.2015 / 18:56