Autocomplete + PHP + MYSQL

Asked

Viewed 137 times

0

I’m running some autocomplete tests to put in a form on my website, but when I run the autocomplete, it doesn’t work. I even searched for possible errors that occur in the autocomplete and found nothing.

The files are these below:

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>

usuarios.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;

        }
    }
?>

1 answer

1

You can already see that there is an error in the variable of your query: 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";
     }
 }
  • I made the correction. Still no spin.

  • ok, but now the reason is another right. from what I’m seeing, you’re not returning anything. have to print out ok.

  • Yes. The problem is that I can’t identify the error.

  • you need to print this: echo implode("|",$dados) or use a echo json_encode($dados); see, what the library asks for, how it wants the structure to be received...

  • So I switched to these two echo for the test and unsuccessfully!.

Browser other questions tagged

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