Autocomplete + PHP + MYSQL

0

I'm doing autocomplete tests to put in a form on my site, however, when I run autocomplete, it does not work. I even searched for possible errors that occur in autocomplete and found nothing.

The files are as follows:

ajax.php

<?php

include_once 'usuarios.class.php';

$usuario = new Usuarios();

echo json_encode ($usuario->buscarMatric($_GET['term']));

?>

complete.php

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link type="text/css" rel="stylesheet" href="_css/jquery-ui-1.8.4.custom.css"/>
        <script type="text/javascript" src="_js/jquery-1.4.2.min.js"></script>
        <script type="text/javascript" src="_js/jquery-ui-1.8.4.custom.min.js"></script>
        <script type="text/javascript">
            $(function(){
                $ ('#buscar_usuario').autocomplete ({
                    source : 'ajax.php';
                    select : function (event, ui) {
                        $ ('#resultados').slideUp ('slow', function () {
                            $ ('#resultados').html (
                                '<h2>Detalhes</h2>' +
                                '<strong>Matrícula: </strong>' + ui.item.value + '<br/>' + 
                                '<strong>Nome:</strong>' + ui.item.descricao + '<br/>' +
                                '<strong>Tipo:</strong>' + ui.item.tipo +  '<br/>' + 
                                '<strong>Data de Nascimento</strong>' + ui.item.nascimento + '<br/>' +

                        );
                    });
                    $ ('#resultados').slideDown ('slow');

            }                   
                });
            });
        </script>
        <title>TESTANDO O AUTOCOMPLETE</title>
    </head>

    <body>
        <div id="buscar">
            <input type="text" id="buscar_usuario" name="buscar_usuario" />
        </div>

        <div id="resultados">

        </div>

    </body>
    </html>

users.class.php

<?php
 class Usuarios
{
    public function __construct () {
        $dbhost = 'localhost';
        $dbuser = 'cnsmaxim_inezb';
        $dbpass = 'testando';
        $dbname = 'dbteste';

        mysql_connect($dbhost, $dbuser, $dbpass);

        mysql_select_db ($dbname);

    }
        public function buscarMatricula ($numMatricula) {
            $dados = array ();

            $sql = "SELECT * FROM DBWEBCAD
                    WHERE MATRIC LIKE '%numMatricula%'
                    OR DTADM LIKE '%numMatricula%'";


            $resultado = mysql_query ($sql);

            while ($row = mysql_fetch_array($resultado, MYSQL_ASSOC)){ 
                $dados [] = array ("value" => $row ['MATRIC'] . ' '.
                                              $row ['DTADM'],
                                    "descricao" => $row ['NOMESOCDEP'],
                                    "tipo" => $row ['TIPOSOC'],
                                    "nascimento" => $row ['DTNASC']);
            }
            return;

        }
    }
?>
    
asked by anonymous 14.10.2016 / 19:55

1 answer

1

You can already see that there is an error in your query variable: numMatricula = > $ numMatricula:

 "SELECT * FROM DBWEBCAD
                    WHERE MATRIC LIKE '%$numMatricula%'
                    OR DTADM LIKE '%$numMatricula%'";

You can do something like this:

$sql = "SELECT * FROM DBWEBCAD
                    WHERE MATRIC LIKE '%$numMatricula%'
                    OR DTADM LIKE '%$numMatricula%'";

 $resultado = mysql_query ($sql);
 $collection = array();
 while ($row = mysql_fetch_array($resultado, MYSQL_ASSOC)){ 
       $collection[] = $row;
 }

if (!empty($collection)) {
     foreach ($collection as $k => $result) {
        echo  $k . '|' . $result . "\n";
     }
 }
    
14.10.2016 / 20:39