Advertisement
Guest User

Untitled

a guest
Nov 8th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. <?php
  2. $host="localhost"; // Host name
  3. $username="testecod"; // Mysql username
  4. $password="pass"; // Mysql password
  5. $db_name="testando"; // Database name
  6.  
  7.  
  8. $con = mysql_connect($host,$username,$password) or die(mysql_error());
  9. mysql_select_db($db_name, $con) or die(mysql_error());
  10.  
  11. $q = strtolower($_GET["q"]);
  12. if (!$q) return;
  13.  
  14. $sql = "select DISTINCT MATRIC from DBWEBCAD where MATRIC LIKE '%$q%'";
  15. $rsd = mysql_query($sql);
  16. while($rs = mysql_fetch_array($rsd)) {
  17. $cmat = $rs['MATRIC'];
  18. $cname = $rs ['NOMSCODEP'];
  19. echo "$cmatn", "$cnamen";
  20. }
  21. ?>
  22.  
  23. <?php
  24. session_start();
  25. ?>
  26. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  27. <html>
  28. <head>
  29.  
  30. <title>jQuery Autocomplete Plugin</title>
  31. <script type="text/javascript" src="_js/jquery-1.4.2.js"></script>
  32. <script type='text/javascript' src="_js/jquery.autocomplete.js"></script>
  33. <link rel="stylesheet" type="text/css" href="_js/jquery.autocomplete.css" />
  34. <script type="text/javascript">
  35. $().ready(function() {
  36. $("#course").autocomplete("autoComplete.php", {
  37. width: 260,
  38. matchContains: true,
  39. //mustMatch: true,
  40. //minChars: 0,
  41. //multiple: true,
  42. //highlight: false,
  43. //multipleSeparator: ",",
  44. selectFirst: false
  45. });
  46. });
  47. </script>
  48. </head>
  49. <body>
  50. <h2>Autocomplete usando jQuery, Ajax, PHP</h2>
  51. <div id="content">
  52. <form autocomplete="off">
  53. <p>
  54. Digite um nome:
  55. <input type="text" name="course" id="course" />
  56. </p>
  57.  
  58. </form>
  59. </div>
  60. </body>
  61. </html>
  62.  
  63. <?php
  64.  
  65. include_once ('conecta_mysql.inc');
  66. /**
  67. * função que devolve em formato JSON os dados do cliente
  68. */
  69. function retorna( $MATRIC, $db )
  70. {
  71. $sql = "SELECT `DTADM`, `NOMSOCDEP`, `TIPOSOC`, `DTNASC`
  72. FROM `DBWEBCAD` WHERE `MATRIC` = '{$MATRIC}' ";
  73.  
  74. $query = $db->query( $sql );
  75.  
  76. $arr = Array();
  77. if( $query->num_rows )
  78. {
  79. while( $dados = $query->fetch_object() )
  80. {
  81. $arr['DTADM'] = $dados->cDadm;
  82. $arr['NOMSOCDEP'] = $dados->tNome;
  83. $arr['TIPOSOC'] = $dados->tSoc;
  84. $arr['DTNASC'] = $dados->tNasc;
  85. }
  86. }
  87. else
  88. $arr['tNome'] = 'Não encontrado';
  89.  
  90. return json_encode( $arr );
  91. }
  92.  
  93. /* só se for enviado o parâmetro, que devolve os dados */
  94. if( isset($_GET['MATRIC']) )
  95. {
  96. $db = new mysqli('localhost', 'testecod', 'pass', 'testando');
  97. echo retorna( filter ( $_GET['MATRIC'] ), $db );
  98. }
  99.  
  100. function filter( $var ){
  101. return $var;//a implementação desta, fica a cargo do leitor
  102. }
  103.  
  104. ?>
  105.  
  106. <html>
  107. <head>
  108. <script type="text/javascript" src="_js/jquery.min.js"></script>
  109. <script type="text/javascript">
  110. $(document).ready(function(){
  111. $("input[name='MATRIC']").blur(function(){
  112. var $DTADM = $("input[name='cDadm']");
  113. var $NOMSOCDEP = $("input[name='tNome']");
  114. var $TIPOSOC = $("input[name='tSoc']");
  115. var $DTNASC = $("input[name='tNasc']");
  116.  
  117. $cDadm.val('Carregando...');
  118. $tNome.val('Carregando...');
  119. $tSoc.val('Carregando...');
  120. $tNasc.val('Carregando...');
  121.  
  122. $.getJSON(
  123. 'function.php',
  124. { MATRIC: $( this ).val() },
  125. function( json )
  126. {
  127. $cDadm.val( json.cDadm );
  128. $tNome.val( json.tNome );
  129. $tSoc.val( json.tSoc );
  130. $tNasc.val( json.tNasc );
  131.  
  132. }
  133. );
  134. });
  135. });
  136. </script>
  137. </head>
  138. <body>
  139. <form action="" method="post">
  140. <label>Matrícula: <input type="text" name="MATRIC" /></label><br>
  141. <br>
  142. <label>Data de Admissão: <input name="cDadm" type="text" value="" /></label><br>
  143. <br>
  144. <label>Nome: <input type="text" name="tNome" value="" /></label><br>
  145. <br>
  146. <label>Tipo: <input type="text" name="tSoc" value="" /></label><br>
  147. <br>
  148. <label>Data de Nascimento: <input type="text" name="tNasc" value="" /></label><br>
  149.  
  150. </form>
  151. </body>
  152. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement