我想在网络上使用选择功能和搜索功能。并希望在网络上显示人们的DB数据表列。
如果我在$ sql中使用'or',则可以使用它的搜索功能。如果我在$ sql中使用'and',则可以使用select函数。
但是我想同时使用这两个功能。我的代码有什么问题?如何获得两种功能?
这是“ load_data_select.php”脚本代码。
<script>
$(document).ready(function(){
function load_data(query)
{
$.ajax({
url:"load_data.php",
method:"POST",
data:{query:query},
dataType:"text",
success:function(data)
{
$('#show_product').html(data);
}
});
}
$('#search_text').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else {
load_data(search);
}
});
$('#Category').change(function(){
var Category = $(this).val();
$.ajax({
url:"load_data.php",
method:"POST",
data:{Category:Category},
success:function(data)
{
$('#show_product').html(data);
}
});
});
$('#Obtain').change(function(){
var Obtain = $(this).val();
$.ajax({
url:"load_data.php",
method:"POST",
data:{Obtain:Obtain},
success:function(data)
{
$('#show_product').html(data);
}
});
});
});
</script>
这是load_data.php。如果将“或类别”更改为“与类别”,则可以选择功能。但无法使用搜索功能。
$sql = "SELECT * FROM 190406test2
WHERE Name LIKE '%".$_POST["query"]."%'
or Category = '".$_POST["Category"]."'
or Obtained_From = '".$_POST["Obtain"]."'
";
$result = mysqli_query($conn, $sql);
if(isset($_POST["query"]) or
isset($_POST["Category"]) or isset($_POST["Obtain"])
)
{
if($_POST["query"] != ''
or $_POST["Category"] != '' or $_POST["Obtain"] != '')
{
$sql;
}
else
{
$sql = "SELECT * FROM 190406test2
";
}
我希望可以使用工作选择功能和搜索功能,但实际输出仅是选择功能或搜索功能。
1)要同时使用这两个功能,您需要一个新按钮:
使用ajax时,实际上不需要表单标签。您可以通过以下方式将输入字段打包在div-inputscontainer中:
<div id="inputs_container">
<input type="text" id="inp1"></input>
<input type="text" id="inp2"></input>
<input type="text" id="inp3"></input>
<button id="submit_btn">Save</button>
</div>
<script>
$(document).ready(function() {
$("#submit_btn").click(function() {
var Obj_allinputs = new Object();
$("#inputs_container input[type=text]").each(function() {
Obj_allinputs[this.id]=this.value;
});
$.ajax({
url:"load_data.php",
method:"POST",
data:Obj_allinputs,
success:function(data)
{
$('#show_product').html(data);
}
}</script>
2)在load_data.php中:
$sql='SELECT * FROM 190406test2';
if(count($_POST)>0) $sql.=' WHERE '
//if(isset($_POST["search"])) sql.=" Name LIKE '%?%' OR ";
$ar_bindparam=array();
foreach ($_POST as $key=>$val)
{
$ar_bindparam[$key] = $_POST[$val];
if($key=="search") sql.=" $key LIKE '%?%' OR ";
else sql.=" $key = '?' OR ";
}
$sql = preg_replace('#(\sOR\s)$#', '', $sql);
//$result = mysqli_query($conn, $sql); //weak regarding injection
//$db = new mysqli("localhost","root","","tests");
if( $stmt = $db->prepare($sql) )
{ //stmt is of type mysqli_stmt
if(count($ar_bindparam)>0)
{
$ar_bindparam = array_merge(array(str_repeat('s', count($ar_bindparam))), array_values($ar_bindparam));
$ref = new ReflectionClass('mysqli_stmt');
$method = $ref->getMethod("bind_param");
$method->invokeArgs($stmt,$ar_bindparam);
//FOR php version < 5.3 : call_user_func_array(array(&$stmt, 'bind_param'), $ar_bindparam);
}
$smt->execute();
//echo $result in the right format back to your ajax call
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句