0

In my database I have a list of city and I want to use them for an autocompletion textfield, but it doesn't work and I don't know what is wrong ?

Here is my textfield in html:

<input name="signup_ville" id="signup_city" class="form-control" type="text" placeholder="Ville" required />

In the JS script I only add the source fot the php file with the request:

$('#signup_city').autocomplete({
    source : './Application/Script/liste_ville.php'
});

And this is my php file.

<?php
  include_once 'config.inc.php';

  $term = $_GET['term'];
  $termParse = '%'.$term.'%'
  $requete = $PDO_BDD->query("SELECT * FROM ville WHERE nom LIKE '$termParse'");
  $array = array(); // on créé le tableau

  while($donnee = $requete->fetch())
  {
      array_push($array, $donnee['nom']);
      echo "test";
  }

  echo json_encode($array);

?>

I don't even know if that file is called by the JS function because the echo "test" doesn't display anything

Clement Cuvillier
  • 227
  • 1
  • 7
  • 24
  • Your script is at risk of [SQL Injection Attack](https://stackoverflow.com/q/60174/5914775). Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/). Even [if you are escaping inputs, its not safe!](https://stackoverflow.com/q/36628418/5914775). Use [prepared parameterized statements](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – Tom Udding May 06 '17 at 15:15
  • You are also missing a header, see [Returning JSON from a PHP Script](http://stackoverflow.com/questions/4064444/returning-json-from-a-php-script) – Tom Udding May 06 '17 at 15:17
  • Ok I change my query by a prepared query and I add the header for returning JSON but it still doesn't work. I don't know if the query is executed, and when I `print_r()` the result of the query there is nothing. – Clement Cuvillier May 06 '17 at 15:58
  • I use Smarty, maybe there is something special to do ? – Clement Cuvillier May 08 '17 at 19:06
  • Not that I know of, you could check the documentation how you can check if the query returned something, if it doesn't return something then there is something wrong with the query. – Tom Udding May 09 '17 at 05:30

0 Answers0