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
index1.php
<table id="employees"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Age</th> <th>Address</th> <th>Car</th> <th>Language</th> <th>Nights</th> <th>Student</th> </tr> </thead> <tbody> </tbody> </table> <div id="filter"> <h2>Filter options</h2> <div> <input type="checkbox" id="car" name="hasCar"> <label for="car">Has own car</label> </div> <div> <input type="checkbox" id="language" name="speaksForeignLanguage"> <label for="language">Can speak foreign language</label> </div> <div> <input type="checkbox" id="nights" name="canWorkNights"> <label for="nights">Can work nights</label> </div> <div> <input type="checkbox" id="student" name="isStudent"> <label for="student">Is a student</label> </div> </div> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> //get the form data using another method var miasto = $("input#miasto").val(); dataString = "miasto=" + miasto; $.ajax({ type: "POST", url: "submit.php", dataType : 'json', data: dataString, }); return false; function makeTable(data){ var tbl_body = ""; var tbl_row = ""; tbl_row += "<td>"+v+"</td>"; }) tbl_body += "<tr>"+tbl_row+"</tr>"; }) return tbl_body; } function getEmployeeFilterOptions(){ var opts = []; if(this.checked){ opts.push(this.name); } }); return opts; } function updateEmployees(opts){ $.ajax({ type: "POST", url: "submit.php", dataType : 'json', cache: false, data: {filterOpts: opts}, success: function(records){ $('#employees tbody').html(makeTable(records)); } }); } var $checkboxes = $("input:checkbox"); $checkboxes.on("change", function(){ var opts = getEmployeeFilterOptions(); updateEmployees(opts); }); updateEmployees(); </script>
submit.php
<?php $miasto=$_GET['miasto']; $pdo = new PDO('mysql:host=localhost;dbname=baza', 'user', 'haslo'); $select = 'SELECT *'; $from = ' FROM baza'; $where = ' WHERE miasto = "' . $miasto . '" TRUE'; $where .= " AND hasCar = 1"; } $where .= " AND speaksForeignLanguage = 1"; } $where .= " AND canWorkNights = 1"; } $where .= " AND isStudent = 1"; } $sql = $select . $from . $where; $statement = $pdo->prepare($sql); $statement->execute(); $results = $statement->fetchAll(PDO::FETCH_ASSOC); $json = json_encode($results); ?>
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.