我正在尝试使用PHP和AJAX在表中编写搜索结果,但到目前为止我还没有能够这样做。每当我按下搜索按钮时,页面都会刷新,我无法调试。但是,当我删除对php文件的ajax请求时,我能够确定“dienst”和“ziekte”的值存储在js文件中(它不会刷新它应该)但是每当我尝试访问时然而,这不再起作用,因为它开始刷新(所有代码unver var request = ....
目标是:从表单中我获得2个值(“dienst”和“ziekte”),我使用这些值来运行一个简单的选择查询,该查询应该返回几行。然后应将这些行转换为html中的表格作为搜索结果。
代码:
search.js
档案:
$('#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
档案:
<?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();
}
}
?>
结果的html文件:
<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>
SearchPatient功能
public function SearchPatient()
{
$db = new db('localhost', 'root', '', 'project');
$res = $db->Search( "'" . $this->Dienst . "'", "'" . $this->Ziekte . "'" , "patient_tbl");
return $res;
}
数据库类:
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;
}
}
答案 0 :(得分:1)
在search.js中,$ .ajax数据应该是查询字符串(数据:{'dienst ='+ dienst +'&amp; ziekte ='ziekte})或对象(数据) :{dienst : dienst,ziekte : ziekte},)