douchengchu8374 2014-04-17 13:06
浏览 30

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

Hello and thank you for readying my post. Im struggling with the question how to get my results on the screen via AJAX. I have a textbox and I want to make a selection in my table whenever something gets typed in (basicly selected from the autocomplete list). I cant seem to get it to work so that it shows the results on the screen via my table.

Im stuck on how to do this, any help would be great :)

This is my code at the moment:

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条回答 默认 最新

  • douzhi3454 2014-04-17 13:15
    关注

    I don't really understand what are you doing, but maybe you need something like this:

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

报告相同问题?

悬赏问题

  • ¥15 is not in the mmseg::model registry。报错,模型注册表找不到自定义模块。
  • ¥15 安装quartus II18.1时弹出此error,怎么解决?
  • ¥15 keil官网下载psn序列号在哪
  • ¥15 想用adb命令做一个通话软件,播放录音
  • ¥30 Pytorch深度学习服务器跑不通问题解决?
  • ¥15 部分客户订单定位有误的问题
  • ¥15 如何在maya程序中利用python编写领子和褶裥的模型的方法
  • ¥15 Bug traq 数据包 大概什么价
  • ¥15 在anaconda上pytorch和paddle paddle下载报错
  • ¥25 自动填写QQ腾讯文档收集表