Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Ajax多文本框搜索请求_Php_Html_Ajax - Fatal编程技术网

Php Ajax多文本框搜索请求

Php Ajax多文本框搜索请求,php,html,ajax,Php,Html,Ajax,你好,我正在尝试通过AJAX让我的文本框与数据库中的搜索选项一起工作 我没有让它在多个文本框上工作,只有一个 这是我的密码,也许你们能帮我把它用起来。那太好了 php <?php $pdo = new PDO('mysql:host=localhost;dbname=records', 'root', 'l3tm31n'); $select = 'SELECT *'; $from = ' FROM overboekingen'; $opts = (isset($_POST['filter

你好,我正在尝试通过AJAX让我的文本框与数据库中的搜索选项一起工作

我没有让它在多个文本框上工作,只有一个

这是我的密码,也许你们能帮我把它用起来。那太好了

php

<?php
$pdo = new PDO('mysql:host=localhost;dbname=records', 'root', 'l3tm31n');
$select = 'SELECT *';
$from = ' FROM overboekingen';

$opts = (isset($_POST['filterOpts']) ? $_POST['filterOpts'] : FALSE);
$val = (isset($_POST['text']) ? $_POST['text'] : FALSE);

if ($val != null){
  $where = " WHERE boekingsnummer LIKE '".$val."%'";
}
elseif ($val != null){
  $where = " AND huiscode LIKE '".$val."%'";
}
else {

  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);
编辑:
我想您可能希望像这样调用JavaScript函数和PHP脚本: JavaScript

$('#boekingsnummer_1').keyup(function(){        
    updateEmployeesText($(this).val(),'boekingsnummer');        
});

$('#huiscode_1').keyup(function(){        
    updateEmployeesText($(this).val(),'huiscode');        
});

function updateEmployeesText(val,opt){        
    $.ajax({
    type: "POST",
    url: "submit.php",
    dataType : 'json',
    cache: false,
    data: {text: val, filterOpts:opt},
    success: function(records){
        $('#employees tbody').html(makeTable(records));
    }        
}); 
}
PHP:


你能和我们分享你的HTML和JavaScript吗?首先FALSE不为NULL。@naota确定我已经用我使用的HTML和AJAX代码更新了代码。HTML代表两个文本框,AJAX代表javascript部分。@user3541335,谢谢。到目前为止你有什么错误?@naota Wel基本上我没有得到任何eror,2e文本框使用的是与文本框1相同的sql语句。娜奥塔:对,这似乎是这里的问题,这样第二个文本字段就不会被删除了applied@user3541335:尝试在ajax代码中添加类型(booking或huiscode),然后根据此类型创建sql-query@Gizzmo还有娜奥塔,因此,我必须将elseif结构更改为and'IF'+'和'construction?@user3541335,这取决于您希望用SQL实现什么。@naota Wel我想在每个文本框中获得另一个SQL语句,该语句的结果与mysql不同。当您在文本框中键入内容时,每个文本框都应该从数据库中获得自己的结果:)
$('#boekingsnummer_1').keyup(function(){        
    updateEmployeesText($(this).val());        
});

$('#huiscode_1').keyup(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));
    }        
}); 
}
$('#boekingsnummer_1').keyup(function(){        
    updateEmployeesText($(this).val(),'boekingsnummer');        
});

$('#huiscode_1').keyup(function(){        
    updateEmployeesText($(this).val(),'huiscode');        
});

function updateEmployeesText(val,opt){        
    $.ajax({
    type: "POST",
    url: "submit.php",
    dataType : 'json',
    cache: false,
    data: {text: val, filterOpts:opt},
    success: function(records){
        $('#employees tbody').html(makeTable(records));
    }        
}); 
}
$opts = (isset($_POST['filterOpts']) ? $_POST['filterOpts'] : FALSE);
$val = (isset($_POST['text']) ? $_POST['text'] : FALSE);

if (($val != FALSE) && ($opts == "boekingsnummer")){
  $where = " WHERE boekingsnummer LIKE '".$val."%'";
}elseif (($val != FALSE) && ($opts == "huiscode" )){
  $where = " WHERE huiscode LIKE '".$val."%'";
}