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

时间:2014-04-17 13:06:54

标签: php html ajax

您好,感谢您准备好我的帖子。我正在努力解决如何通过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

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

1 个答案:

答案 0 :(得分:0)

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

<?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);
?>