$.ajax 进行模糊查询

html代码

<div class="conditions staff ue-clear">
<label>公司类型:</label>
<input type="text" id="companytypename"/>
</div>

$.ajax代码

$.ajax({
type: "Post",
url: "/company/companycheck/getacnames",
data: {table:"dictecontypes",code:"code",name:"enterregtype",page: 1, pagesize: 10000},
success: function (res) {
var r = $.parseJSON(res);
if (r != null && r.ret == 0) {
$("#companytypename").autocomplete(r.aaData, {
max: 12, //列表里的条目数
minChars: 0, //自动完成激活之前填入的最小字符
width: 200, //提示的宽度,溢出隐藏
scrollHeight: 300, //提示的高度,溢出显示滚动条
matchContains: true,
autoFill: false,
formatItem: function (row, i, max) {
return row.name;
},
formatMatch: function (row, i, max) {
return row.name;
},
formatResult: function (row) {
return row.name;
}
}).result(function (event, row, formatted) {
$("#companytypename").val(row.name);
});
}
else {
alert(r.msg);
}
}
});

PHP代码
public  function _addWhere($sql,$params,$filerflag=0){
if ((sizeof($params) == 0)||($params == False)) return $sql;
$sql = "SELECT * FROM ($sql) t WHERE 1=1 ";
foreach($params as $key => $value){
if ((substr($value,0,1)=="%")&&(substr($value,1)!= "" ))
$sql .= " and ".substr($key,1)." like '%%".substr($value,1)."%%' ";
if ((substr($value,0,1)=="=")&&(substr($value,1)!= "" ))
$sql .= " and ".substr($key,1)." = '".substr($value,1)."' ";
if ((substr($value,0,1)==">")&&(substr($value,1)!= "" ))
$sql .= " and ".substr($key,1)." >= '".substr($value,1)."' ";
if ((substr($value,0,1)=="<")&&(substr($value,1)!= "" ))
$sql .= " and ".substr($key,1)." <= '".substr($value,1)."' ";
if ((substr($value,0,1)=="!")&&(substr($value,1)!= "" ))
$sql .= " and ".substr($key,1)." != '".substr($value,1)."' ";
if ((substr($value,0,1)=="~")&&(substr($value,1)!= "" )){
$list = explode('|',substr($value,1));
$tmp = "";
foreach($list as $arr) $tmp .= ";'".$arr."'";
$tmp = substr($tmp,1);
$sql .= " and ".substr($key,1)." not in (".substr($value,1).") ";
}
}
//print_r($sql);exit();
return $sql;
}
public function getacnames()
{
if (!isset($_POST["table"])) {
$re["ret"] = 1;
$re["msg"] = "no table!";
echo json_encode($re);
exit;
}
if (!isset($_POST["name"])) {
$re["ret"] = 1;
$re["msg"] = "no name!";
echo json_encode($re);
exit;
}
$aColumns = array();
$table = $_POST["table"];
unset($_POST["table"]);
if (isset($_POST["code"])) {
$code = $_POST["code"];
unset($_POST["code"]);
$aColumns[] = $code;
}
$name = $_POST["name"] . " as name";
unset($_POST["name"]);
$aColumns[] = $name;
$arr = array();
if (isset($_POST["page"]) && isset($_POST["pagesize"])) {
$page = $_POST["page"];
$pagesize = $_POST["pagesize"];
unset($_POST["page"]);
unset($_POST["pagesize"]);
foreach ($_POST as $key => $value) {
if ("token" != $key) {
$arr["%" . $key] = "%" . $value;
}
}
$_POST["sEcho"] = $page;
$_POST["iDisplayLength"] = $pagesize;
$_POST["iDisplayStart"] = $pagesize * ($page - 1);
}
if (!is_numeric($_POST["sEcho"])) {
$result["ret"] = 1;
$result["msg"] = "page number is not number";
}
if (!is_numeric($_POST["iDisplayLength"])) {
$result["ret"] = 1;
$result["msg"] = "page size is not number";
}
if (!is_numeric($_POST["iDisplayStart"])) {
$result["ret"] = 1;
$result["msg"] = "start number is not number";
}
$sql = "select * from " . $table;
$sql = $this->_addWhere($sql, $arr);
$data = $this->mydb->pagingServer($aColumns, $sql);
echo $this->_ReturnMsg($this->_beforeMethod($data['obj']));
}

 

原文地址:https://www.cnblogs.com/lqw4/p/4635621.html