Take Mysql data with PHP and move to Javascript

Asked

Viewed 3,086 times

1

I am trying to use jquery to make 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 one, no:

<script>
$(document).ready(function(){
var availableTags = <?php print(json_encode($potential)); ?>;
console.log(myArray);
$( "#tags" ).autocomplete({
  source: availableTags
});
});
</script>
  • Try replacing json_encode with JSON.stringify(json); var availableTags = <?php print(JSON.stringify($potential)); ?>;

  • Do not mix Mysqli functions with mysql_*

  • Eduardo, it didn’t work. Rray, I fixed it but it seems that this was not the problem. Should I use some plugin? I was thinking of using Lect2 once I would like to create "tags" with each selection.

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 field tag:

<?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 exit is something like that:

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

So for your script to work, you should receive the parameter through a request to your file: seu_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>

0

I got it to work.

Code complete.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 .= ']';



?>

Don’t forget to call the complete.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 auto complete? I want this because I will be working with a database of 30k lines, and I believe this is necessary. jquery autocomplete already has this pre-defined condition?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.