Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [AJAX] przesłanie danych z formularza do innego pliku
Forum PHP.pl > Forum > XML, AJAX
mariuszzzzzz
Witam

Chciałem przerobić swoją wyszukiwarkę bazy danych. Chciałem mieć wyszukiwarkę z możliwością zastosowania filtra na bazie ajax. Nie jestemspecjalistą w tej dziedzinie, dlatego szukałem przykładów i testowałem. A oto co w tym momencie mam:

formularz
  1. <form action="index1.php" method="post" enctype="multipart/form-data">
  2. <input class="gdzie" type="text" name="miasto" value="" />
  3. <input type="image" class="search_btn" src="images/szukaj.png" alt="Szukaj" />
  4. </form>


index1.php
  1. <table id="employees">
  2. <thead>
  3. <tr>
  4. <th>ID</th>
  5. <th>Name</th>
  6. <th>Age</th>
  7. <th>Address</th>
  8. <th>Car</th>
  9. <th>Language</th>
  10. <th>Nights</th>
  11. <th>Student</th>
  12. </tr>
  13. </thead>
  14. <tbody>
  15. </tbody>
  16. </table>
  17.  
  18. <div id="filter">
  19. <h2>Filter options</h2>
  20. <div>
  21. <input type="checkbox" id="car" name="hasCar">
  22. <label for="car">Has own car</label>
  23. </div>
  24. <div>
  25. <input type="checkbox" id="language" name="speaksForeignLanguage">
  26. <label for="language">Can speak foreign language</label>
  27. </div>
  28. <div>
  29. <input type="checkbox" id="nights" name="canWorkNights">
  30. <label for="nights">Can work nights</label>
  31. </div>
  32. <div>
  33. <input type="checkbox" id="student" name="isStudent">
  34. <label for="student">Is a student</label>
  35. </div>
  36. </div>
  37.  
  38. <script src="http://code.jquery.com/jquery-latest.js"></script>
  39. <script>
  40.  
  41. //get the form data using another method
  42. var miasto = $("input#miasto").val();
  43. dataString = "miasto=" + miasto;
  44. $.ajax({
  45. type: "POST",
  46. url: "submit.php",
  47. dataType : 'json',
  48. data: dataString,
  49. });
  50. return false;
  51.  
  52. function makeTable(data){
  53. var tbl_body = "";
  54. $.each(data, function() {
  55. var tbl_row = "";
  56. $.each(this, function(k , v) {
  57. tbl_row += "<td>"+v+"</td>";
  58. })
  59. tbl_body += "<tr>"+tbl_row+"</tr>";
  60. })
  61.  
  62. return tbl_body;
  63. }
  64.  
  65. function getEmployeeFilterOptions(){
  66. var opts = [];
  67. $checkboxes.each(function(){
  68. if(this.checked){
  69. opts.push(this.name);
  70. }
  71. });
  72.  
  73. return opts;
  74. }
  75.  
  76. function updateEmployees(opts){
  77. $.ajax({
  78. type: "POST",
  79. url: "submit.php",
  80. dataType : 'json',
  81. cache: false,
  82. data: {filterOpts: opts},
  83. success: function(records){
  84. $('#employees tbody').html(makeTable(records));
  85. }
  86. });
  87. }
  88.  
  89. var $checkboxes = $("input:checkbox");
  90. $checkboxes.on("change", function(){
  91. var opts = getEmployeeFilterOptions();
  92. updateEmployees(opts);
  93. });
  94.  
  95. updateEmployees();
  96. </script>


submit.php
  1. <?php
  2. $miasto=$_GET['miasto'];
  3. $pdo = new PDO('mysql:host=localhost;dbname=baza', 'user', 'haslo');
  4. $select = 'SELECT *';
  5. $from = ' FROM baza';
  6. $where = ' WHERE miasto = "' . $miasto . '" TRUE';
  7. $opts = isset($_POST['filterOpts'])? $_POST['filterOpts'] : array('');
  8.  
  9. if (in_array("hasCar", $opts)){
  10. $where .= " AND hasCar = 1";
  11. }
  12.  
  13. if (in_array("speaksForeignLanguage", $opts)){
  14. $where .= " AND speaksForeignLanguage = 1";
  15. }
  16.  
  17. if (in_array("canWorkNights", $opts)){
  18. $where .= " AND canWorkNights = 1";
  19. }
  20.  
  21. if (in_array("isStudent", $opts)){
  22. $where .= " AND isStudent = 1";
  23. }
  24.  
  25. $sql = $select . $from . $where;
  26. $statement = $pdo->prepare($sql);
  27. $statement->execute();
  28. $results = $statement->fetchAll(PDO::FETCH_ASSOC);
  29. $json = json_encode($results);
  30. echo($json);
  31. ?>


Problem polega na tym, że jakoś nie dociera do skryptu to co wpisze w formularz, a co za tym idzie nie pokazuje wyników z bazy. Gdzie jest błąd? Prosze was o pomoc.
kamilo818
Wysyłasz Post a odbierasz get
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.