Reputation: 45
I'm trying to write search results in a table using PHP and AJAX but so far I have not been able to do so. Whenever I press the search button the page refreshes and I'm unable to debug. However when I remove the ajax request to the php file I'm able to determine that the values of "dienst" and "ziekte" are stored in the js file (it doesn't refresh as it should) but whenever I try to access the php file however this doesn't work anymore because it starts refreshing (all the code unver var request=....
The goal is: from a form I get 2 values ( "dienst" and "ziekte" ), I use these values to run a simple select query that should return a couple of rows. These rows should then be translated in tablerows in html as a search result.
The code:
search.js
file:
$('#searchButton').on('click', function() {
event.preventDefault();
var dienst = $('[name="dienst"]').val();
var ziekte = $('[name="dienst"]').val();
var request = $.ajax({
url: "ajax/searchresult.php",
type: "POST",
data: { 'dienst=' + dienst + 'ziekte=' ziekte },
dataType: "json"
});
request.done(function( msg ) {
$('#searchresults').html(msg);
});
request.fail(function( jqXHR, textStatus ) {
alert( "Request failed: " + textStatus );
});
});
searchresult.php
file:
<?php
include_once("classes/Db.class.php");
include_once("classes/Patient.class.php");
if (isset($_POST['ziekte'])) {
try
{
$patient = new Patient();
$patient->Dienst = $_POST['dienst'];
$patient->Ziekte = $_POST['ziekte'];
$results = $patient->SearchPatient();
while($r = $results->fetch_assoc()){
echo "<tr>";
echo "<td><a href='profiel.php?username=".$r["username"]."'>". $r["voornaam"] . "</a></td>";
echo "<td><a href='profiel.php?username=".$r["username"]."'>". $r["achternaam"] . "</a></td>";
echo "<td><a href='profiel.php?username=".$r["username"]."'>". $r["username"] . "</a></td>";
echo "<td><a href='profiel.php?username=".$r["username"]."'>". $r["leeftijd"] . "</a></td>";
echo "<td><a href='profiel.php?username=".$r["username"]."'>". $r["beroep"] . "</a></td>";
echo "</tr>";
}
}
catch(Exception $e)
{
$error = $e->getMessage();
}
}
?>
The html file for the result:
<div class="page-tables">
<!-- Table -->
<div class="table-responsive">
<table cellpadding="0" cellspacing="0" border="0" id="data-table" width="100%">
<thead>
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Username</th>
<th>Leeftijd</th>
<th>Beroep</th>
</tr>
</thead>
<tbody id="searchresults">
</tbody>
</table>
<div class="clearfix"></div>
</div>
</div>
</div>
the SearchPatient function
public function SearchPatient()
{
$db = new db('localhost', 'root', '', 'project');
$res = $db->Search( "'" . $this->Dienst . "'", "'" . $this->Ziekte . "'" , "patient_tbl");
return $res;
}
the DB class:
function Search($dienst, $ziekte ,$from)
{
//SELECT *, IF( dienst = 'Psychiatrie', 1,0 ) + IF( ziekte = 'ADHD', 2,0 ) AS score FROM `patient_tbl` ORDER BY score DESC
$query = "SELECT *, IF( dienst = '" . $dienst . "', 1,0 ) + IF( ziekte = '" . $ziekte . "', 2,0 ) AS score FROM `" . $from . " ORDER BY score DESC";
$this->last_query = $query;
if($this->mysqli->query($query))
{
return true;
} else {
return false;
}
}
Upvotes: 0
Views: 1701
Reputation: 26
In search.js the $.ajax data should either be a query string ( data: { 'dienst=' + dienst + '&ziekte=' ziekte } ) or an object ( data: { dienst:dienst, ziekte:ziekte }, )
Upvotes: 1