正在获取页面上显示的AJAX数据库结果


Getting AJAX database results shown on the page

你好,感谢您准备好我的文章。我在纠结如何通过AJAX在屏幕上显示我的结果。我有一个文本框,每当输入内容时(基本上是从自动完成列表中选择的),我都想在表中进行选择。我似乎无法让它工作,所以它通过我的表在屏幕上显示结果。

我纠结于如何做到这一点,任何帮助都会很棒:)

这是我目前的代码:

HTML

<input type="text" name="naam_klant" size="20" id="naam_klant" onkeyup="lookup(this.value);" onblur="fill();" >

PHP

<?php 
$pdo = new PDO('mysql:host=localhost;dbname=records', 'root', '***');
$select = 'SELECT *';
$from = ' FROM overboekingen';
$where2 = ' WHERE naam_klant LIKE %$val% ';
$opts = (isset($_POST['filterOpts']) ? $_POST['filterOpts'] : FALSE);  
$val = (isset($_POST['txt']) ? $_POST['txt'] : FALSE);
  if (is_array($opts) || $val)  {
    $where = ' WHERE FALSE';
}
else {
$where = false;
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>

脚本

<script>
function makeTable(data) {
    var tbl_body = "";
    $.each(data, function() {
        var tbl_row = "";
        $.each(this, function(k , v) {
            tbl_row += "<td>"+v+"</td>";
        })
        tbl_body += "<tr>"+tbl_row+"</tr>";
    })
    return tbl_body;
}
function getEmployeeFilterOptions(){
    var opts = [];
    $checkboxes.each(function(){
        if(this.checked){
            opts.push(this.name);
        }
    });
    return opts;
}
$('naam_klant').change(function(){
updateEmployeesText($(this).val()); 
});
function updateEmployeesText(val){
    $.ajax({
    type: "POST",
    url: "submit.php",
    dataType : 'json',
    cache: false,
    data: {text: val},
    success: function(records){
        $('#employees tbody').html(makeTable(records));
    }
}); 
}
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>

我真的不明白你在做什么,但也许你需要这样的东西:

<?php 
$pdo = new PDO('mysql:host=localhost;dbname=records', 'root', '***');
$select = 'SELECT *';
$from = ' FROM overboekingen';
$opts = (isset($_POST['filterOpts']) ? $_POST['filterOpts'] : FALSE);  
$val = (isset($_POST['txt']) ? $_POST['txt'] : FALSE);
  if (is_array($opts) || $val)  {
    $where = ' WHERE naam_klant LIKE %$val% ';
}
else {
$where = false;
}
$sql = $select . $from . $where;
$statement = $pdo->prepare($sql);
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);
echo($json);
?>