我正在尝试使用PHP和AJAX在表中编写搜索结果,但到目前为止我还没有做到这一点。每当我按下搜索按钮时,页面就会刷新,并且无法调试。但是,当我删除对php文件的ajax请求时,我能够确定“ dienst”和“ ziekte”的值存储在js文件中(它不会按原样刷新),但是无论何时我尝试访问php文件,但是此操作不再起作用,因为它开始刷新(所有代码都为var request = ....

目标是:从一个表单中获得2个值(“ dienst”和“ ziekte”),我使用这些值运行一个简单的select查询,该查询应返回几行。然后,应将这些行转换为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;

    }


DB类:

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;
    }
}

最佳答案

在search.js中,$。ajax数据应该是查询字符串(数据:{''dienst ='+ dienst +'&ziekte ='ziekte})或对象(数据:{dienst:dienst,ziekte:ziekte},)

09-18 21:37