AJAX仅适用于1个文本框

时间:2014-05-15 08:52:01

标签: php jquery html

所以我有这个带有文本框的表单,当您在文本框中键入时,您会从AJAX输出的表格中获得搜索结果。

这适用于1个文本框,但我似乎无法使用其他文本框。

我还在学习如何编程,所以我的代码并不完美,但最重要的是它首先应该有效。

我希望你们能帮助我。

所以这基本上是我的代码如何工作,我省略了包含文本框的HTML部分。

AJAX

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

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

$('#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));
    }        
}); 
}

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."%'";  
    }        
    if ($val != null){            
    $where = " WHERE huiscode LIKE '".$val."%'";  
    }   

    else {

    if (is_array($opts) || $val){

    $where = ' WHERE FALSE';

    if (in_array("medewerker", $opts)){
    $where .= " OR medewerker = 1 ";
  }  
    if (in_array("medewerker1", $opts)){
    $where .= " OR medewerker = 2 ";
  }  
    if (in_array("medewerker2", $opts)){
    $where .= " OR medewerker = 3 ";
  }  
    if (in_array("medewerker3", $opts)){
    $where .= " OR medewerker = 4 ";
  }  
    if (in_array("medewerker4", $opts)){
    $where .= " OR medewerker = 5 ";
  }  
    if (in_array("medewerker5", $opts)){
    $where .= " OR medewerker = 6 ";
  }  
    if (in_array("medewerker6", $opts)){
    $where .= " OR medewerker = 7 ";
  }  
    if (in_array("medewerker7", $opts)){
    $where .= " OR medewerker = 8 ";
  }  
    if (in_array("medewerker8", $opts)){
    $where .= " OR medewerker = 9 ";
  }  
    if (in_array("medewerker9", $opts)){
    $where .= " OR medewerker = 10 ";
  }  
    if (in_array("medewerker10", $opts)){
    $where .= " OR medewerker = 11 ";
  }

    if (in_array("datum_overboeking", $opts)){
    $where .= " OR datum_overboeking = 1";
  }

    if (in_array("juliette_geinformeerd1", $opts)){
    $where .= " OR juliette_geinformeerd = 1";      
  }  
    if (in_array("juliette_geinformeerd2", $opts)){
    $where .= " OR juliette_geinformeerd = 2";      
  }

    if (in_array("land1", $opts)){
    $where .= " OR land = 1";
  }
    if (in_array("land2", $opts)){
    $where .= " OR land = 2";
  }
    if (in_array("land3", $opts)){
    $where .= " OR land = 3";
  }
    if (in_array("land4", $opts)){
    $where .= " OR land = 4";
  }
    if (in_array("land5", $opts)){
    $where .= " OR land = 5";
  }
    if (in_array("land6", $opts)){
    $where .= " OR land = 6";
  }
    if (in_array("land7", $opts)){
    $where .= " OR land = 7";
  }
    if (in_array("land8", $opts)){
    $where .= " OR land = 8";
  }
    if (in_array("land9", $opts)){
    $where .= " OR land = 9";
  }
    if (in_array("land10", $opts)){
    $where .= " OR land = 10";
  }
    if (in_array("land11", $opts)){
    $where .= " OR land = 11";
  }
    if (in_array("land12", $opts)){
    $where .= " OR land = 12";
  }
    if (in_array("land13", $opts)){
    $where .= " OR land = 13";
  }

    if (in_array("aanbieder1", $opts)){
    $where .= " OR aanbieder = 1";
  }
    if (in_array("aanbieder2", $opts)){
    $where .= " OR aanbieder = 2";
  }
    if (in_array("aanbieder3", $opts)){
    $where .= " OR aanbieder = 3";
  }
    if (in_array("aanbieder4", $opts)){
    $where .= " OR aanbieder = 4";
  }

    if (in_array("contractvorm1", $opts)){
    $where .= " OR contractvorm = 1";
  }
    if (in_array("contractvorm2", $opts)){
    $where .= " OR contractvorm = 2";
  }
    if (in_array("contractvorm3", $opts)){
    $where .= " OR contractvorm = 3";
  }

    if (in_array("periode_van", $opts)){
    $where .= " OR periode_van = 1";
  }

    if (in_array("periode_tot", $opts)){
    $where .= " OR periode_tot = 1";
  }

    if (in_array("reden1", $opts)){
    $where .= " OR reden = 1";
  }
    if (in_array("reden2", $opts)){
    $where .= " OR reden = 2";
  }
    if (in_array("reden3", $opts)){
    $where .= " OR reden = 3";
  }
    if (in_array("reden4", $opts)){
    $where .= " OR reden = 4";
  }
    if (in_array("reden5", $opts)){
    $where .= " OR reden = 5";
  }
    if (in_array("reden6", $opts)){
    $where .= " OR reden = 6";
  }
    if (in_array("reden7", $opts)){
    $where .= " OR reden = 7";
  }
    if (in_array("reden8", $opts)){
    $where .= " OR reden = 8";
  }
    if (in_array("reden9", $opts)){
    $where .= " OR reden = 9";
  }
    if (in_array("reden10", $opts)){
    $where .= " OR reden = 10";
  }
    if (in_array("reden11", $opts)){
    $where .= " OR reden = 11";
  }
    if (in_array("reden12", $opts)){
    $where .= " OR reden = 12";
  }
    if (in_array("reden13", $opts)){
    $where .= " OR reden = 13";
  }
    if (in_array("reden14", $opts)){
    $where .= " OR reden = 14";
  }
    if (in_array("reden15", $opts)){
    $where .= " OR reden = 15";
  }
    if (in_array("reden16", $opts)){
    $where .= " OR reden = 16";
  }
    if (in_array("reden17", $opts)){
    $where .= " OR reden = 17";
  }
    if (in_array("reden18", $opts)){
    $where .= " OR reden = 18";
  }  

    if (in_array("nieuwe_boeking1", $opts)){
    $where .= " OR nieuwe_boeking = 1";
  }
    if (in_array("nieuwe_boeking2", $opts)){
    $where .= " OR nieuwe_boeking = 2";
  }
    if (in_array("nieuwe_boeking3", $opts)){
    $where .= " OR nieuwe_boeking = 3";
  }

    if (in_array("reden_geen_nieuwe_boeking1", $opts)){
    $where .= " OR reden_geen_nieuwe_boeking = 1";
  }
    if (in_array("reden_geen_nieuwe_boeking2", $opts)){
    $where .= " OR reden_geen_nieuwe_boeking = 2";
  }
    if (in_array("reden_geen_nieuwe_boeking3", $opts)){
    $where .= " OR reden_geen_nieuwe_boeking = 3";
  }
    if (in_array("reden_geen_nieuwe_boeking4", $opts)){
    $where .= " OR reden_geen_nieuwe_boeking = 4";
  }

    if (in_array("boete_aanbieden1", $opts)){
    $where .= " OR boete_aanbieden = 1";
  }
    if (in_array("boete_aanbieden2", $opts)){
    $where .= " OR boete_aanbieden = 2";
  }

    if (in_array("administratie_geinformeerd_voor_inhouding_boete1", $opts)){
    $where .= " OR administratie_geinformeerd_voor_inhouding_boete = 1";
  }
    if (in_array("administratie_geinformeerd_voor_inhouding_boete2", $opts)){
    $where .= " OR administratie_geinformeerd_voor_inhouding_boete = 2";
  }
    if (in_array("administratie_geinformeerd_voor_inhouding_boete3", $opts)){
    $where .= " OR administratie_geinformeerd_voor_inhouding_boete = 3";
  }

    if (in_array("coulance_happyhome1", $opts)){
    $where .= " OR coulance_happyhome = 1";
  }
    if (in_array("coulance_happyhome2", $opts)){
    $where .= " OR coulance_happyhome = 2";
  }
    if (in_array("coulance_happyhome3", $opts)){
    $where .= " OR coulance_happyhome = 3";
  }
    if (in_array("coulance_happyhome4", $opts)){
    $where .= " OR coulance_happyhome = 4";
  }
    if (in_array("coulance_happyhome5", $opts)){
    $where .= " OR coulance_happyhome = 5";
  }

 }

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

0 个答案:

没有答案