Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host="localhost"; // Host name
- $username="testecod"; // Mysql username
- $password="pass"; // Mysql password
- $db_name="testando"; // Database name
- $con = mysql_connect($host,$username,$password) or die(mysql_error());
- mysql_select_db($db_name, $con) or die(mysql_error());
- $q = strtolower($_GET["q"]);
- if (!$q) return;
- $sql = "select DISTINCT MATRIC from DBWEBCAD where MATRIC LIKE '%$q%'";
- $rsd = mysql_query($sql);
- while($rs = mysql_fetch_array($rsd)) {
- $cmat = $rs['MATRIC'];
- $cname = $rs ['NOMSCODEP'];
- echo "$cmatn", "$cnamen";
- }
- ?>
- <?php
- session_start();
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
- <html>
- <head>
- <title>jQuery Autocomplete Plugin</title>
- <script type="text/javascript" src="_js/jquery-1.4.2.js"></script>
- <script type='text/javascript' src="_js/jquery.autocomplete.js"></script>
- <link rel="stylesheet" type="text/css" href="_js/jquery.autocomplete.css" />
- <script type="text/javascript">
- $().ready(function() {
- $("#course").autocomplete("autoComplete.php", {
- width: 260,
- matchContains: true,
- //mustMatch: true,
- //minChars: 0,
- //multiple: true,
- //highlight: false,
- //multipleSeparator: ",",
- selectFirst: false
- });
- });
- </script>
- </head>
- <body>
- <h2>Autocomplete usando jQuery, Ajax, PHP</h2>
- <div id="content">
- <form autocomplete="off">
- <p>
- Digite um nome:
- <input type="text" name="course" id="course" />
- </p>
- </form>
- </div>
- </body>
- </html>
- <?php
- include_once ('conecta_mysql.inc');
- /**
- * função que devolve em formato JSON os dados do cliente
- */
- function retorna( $MATRIC, $db )
- {
- $sql = "SELECT `DTADM`, `NOMSOCDEP`, `TIPOSOC`, `DTNASC`
- FROM `DBWEBCAD` WHERE `MATRIC` = '{$MATRIC}' ";
- $query = $db->query( $sql );
- $arr = Array();
- if( $query->num_rows )
- {
- while( $dados = $query->fetch_object() )
- {
- $arr['DTADM'] = $dados->cDadm;
- $arr['NOMSOCDEP'] = $dados->tNome;
- $arr['TIPOSOC'] = $dados->tSoc;
- $arr['DTNASC'] = $dados->tNasc;
- }
- }
- else
- $arr['tNome'] = 'Não encontrado';
- return json_encode( $arr );
- }
- /* só se for enviado o parâmetro, que devolve os dados */
- if( isset($_GET['MATRIC']) )
- {
- $db = new mysqli('localhost', 'testecod', 'pass', 'testando');
- echo retorna( filter ( $_GET['MATRIC'] ), $db );
- }
- function filter( $var ){
- return $var;//a implementação desta, fica a cargo do leitor
- }
- ?>
- <html>
- <head>
- <script type="text/javascript" src="_js/jquery.min.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- $("input[name='MATRIC']").blur(function(){
- var $DTADM = $("input[name='cDadm']");
- var $NOMSOCDEP = $("input[name='tNome']");
- var $TIPOSOC = $("input[name='tSoc']");
- var $DTNASC = $("input[name='tNasc']");
- $cDadm.val('Carregando...');
- $tNome.val('Carregando...');
- $tSoc.val('Carregando...');
- $tNasc.val('Carregando...');
- $.getJSON(
- 'function.php',
- { MATRIC: $( this ).val() },
- function( json )
- {
- $cDadm.val( json.cDadm );
- $tNome.val( json.tNome );
- $tSoc.val( json.tSoc );
- $tNasc.val( json.tNasc );
- }
- );
- });
- });
- </script>
- </head>
- <body>
- <form action="" method="post">
- <label>Matrícula: <input type="text" name="MATRIC" /></label><br>
- <br>
- <label>Data de Admissão: <input name="cDadm" type="text" value="" /></label><br>
- <br>
- <label>Nome: <input type="text" name="tNome" value="" /></label><br>
- <br>
- <label>Tipo: <input type="text" name="tSoc" value="" /></label><br>
- <br>
- <label>Data de Nascimento: <input type="text" name="tNasc" value="" /></label><br>
- </form>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement