RyanJ93

Primordiale esempio di autocomplete PHP & MySQL + AJAX

Sep 23rd, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. Primordiale esempio di autocomplete PHP & MySQL + AJAX
  2.  
  3. PHP (result.php):
  4.  
  5. //RICEVO LA CHIAVE DI RICERCA
  6. //controllo che esista altrimenti termino l'esecuzione dello script
  7. if ( isset($_REQUEST['key']) && $_REQUEST['key'] != "" ){
  8. $key = $_REQUEST['key'];
  9. }else{
  10. exit;
  11. }
  12. //RICEVO I DATI DAL DB
  13. //eseguo una query sul database MySQL, se è in uso un database se no si può sostituire questa parte con codice diverso
  14. $query = "SELECT * FROM table WHERE name LIKE '%" . $key . "%';";
  15. $res = mysql_query($query) or die(mysql_error());
  16. //controllo che il numero di dati non sia zero altrimenti termino l'esecuzione dello script
  17. if ( mysql_num_rows($res) <= 0 ){
  18. exit;
  19. }
  20. //USANDO XML
  21. //genero l'output in XML
  22. echo "<?xml version=\"1.0\" ?>\n<xml>\n";
  23. for ( $i = 0 ; $i < mysql_num_rows($res) ; $i++ ){
  24. echo "\t<result>\n";
  25. echo "\t\t<name>" . mysql_result($res, $i, 'name') . "</name>\n";
  26. echo "\t</result>\n";
  27. }
  28. echo "</xml>";
  29. //USANDO JSON
  30. //genero l'output in JSON
  31. echo '{"result":[';
  32. for ( $i = 0 ; $i < mysql_num_rows($res) ; $i++ ){
  33. if ( $i == 0 ){
  34. echo '{"name":"' . mysql_result($res, $i, 'name') . '"}';
  35. }else{
  36. echo ',{"name":"' . mysql_result($res, $i, 'name') . '"}';
  37. }
  38. }
  39. echo ']}';
  40.  
  41.  
  42. JAVASCRIPT:
  43.  
  44. function getData(){
  45. //ottengo il testo inserito nel campo input
  46. var key = $("#input-id").val();
  47. //controllo che non sia vuoto altrimenti termino lo script
  48. if ( key == "" ){
  49. return false;
  50. }
  51. //genero l'URL con l'eventuale parametro GET
  52. var url = "result.php?key=" + key;
  53. var append = "";
  54. //eseguo la chiamata AJAX alla pagina PHP
  55. $.ajax({
  56. url: url,
  57. success: function(data){
  58. //ricevo i dati dalla pagina PHP
  59. //controllo che i dati non siano vuoti altrimenti termino lo script
  60. if ( data == "" ){
  61. return false;
  62. }
  63. //svuoto il contenitore HTML
  64. $("#html-ul-id").html("");
  65. //USANDO XML
  66. $(data).find('result').each(function(){
  67. //per ogni risultato appendo al contenitore HTML il markup
  68. append = '<li>' + $(this).find('name').text() + '</li>';
  69. $("#html-ul-id").append(append);
  70. });
  71. //USANDO JSON
  72. var obj = JSON.parse(data);
  73. for ( var i = 0 ; i < obj.result.length ; i++ ){
  74. //per ogni risultato appendo al contenitore HTML il markup
  75. append = '<li>' + obj.result[i].name + '</li>';
  76. $("#html-ul-id").append(append);
  77. }
  78. return true;
  79. }
  80. });
  81. }
  82.  
  83. //per ogni pressione di un tasto sulla tastiera eseguo la funzione di ricerca
  84. $("#input-id").keyup(function(){
  85. getData();
  86. });
Advertisement
Add Comment
Please, Sign In to add comment