gpt4 book ai didi

php - 使用 php 将搜索中的两个单独的输入字段合并

转载 作者:行者123 更新时间:2023-11-29 21:00:58 25 4
gpt4 key购买 nike

我有两个单独的输入字段,一个用于搜索姓氏,另一个用于搜索数据库的名称。

<li><label>Surname: </label><input type="text" name="surname" id="search_text" required autocomplete="off" /></li>
<li><label>Name: </label><input type="text" name="name" id="name" required autocomplete="off"/></li>



<script> // first input live search
$(document).ready(function() {

$("#search_text").keyup(function () {
var txt = $(this).val();

setTimeout(function() {
if(txt.length>=3)
{
$.ajax({
url:"fetchsurname.php",
method:"post",
data:{search:txt},
dataType:"text",

success:function(data)
{
$('#searchresults').html(data);
}
});
}
else
{
$('#searchresults').html('');
}
}, 2000); // 1 sec delay to check.

}); // End of keyup function

}); // End of document.ready

</script>
<script> // second input live search
$(document).ready(function() {

$("#name").keyup(function () {
var txt = $(this).val();

setTimeout(function() {
if(txt.length>=3)
{
$.ajax({
url:"fetchname.php",
method:"post",
data:{search:txt},
dataType:"text",

success:function(data)
{
$('#searchresults').html(data);
}
});
}
else
{
$('#searchresults').html('');
}
}, 2000); // 1 sec delay to check.

}); // End of keyup function

}); // End of document.ready
</script>

使用 php fetchname 和 fetchsuurname 两个单独的文件搜索数据库

"SELECT * FROM base WHERE name LIKE '".$_POST["search"]."%'"

"SELECT * FROM base WHERE surname LIKE '".$_POST["search"]."%'"

我的问题是每个字段都在表中搜索自己的值,而不是与第一个字段组合来显示结果。

最佳答案

根据您的html,您应该更改 jQuery 并更改为以下代码:

<li><label>Surname: </label><input type="text" name="surname" id="search_text" required autocomplete="off" /></li>

<li><label>Name: </label><input type="text" name="name" id="search_name" required autocomplete="off"/></li>

<script> // first input live search
var keyupfunction = function () {
var search_text = $("#search_text").val();
var search_name = $("#search_name").val();

setTimeout(function() {
if(txt.length>=3)
{
$.ajax({
url:"fetchsurname.php",
method:"post",
data:{"surname":search_text, "name":search_name},
dataType:"text",

success:function(data)
{
$('#searchresults').html(data);
}
});
}
else
{
$('#searchresults').html('');
}
}, 2000); // 1 sec delay to check.

}

$(document).ready(function() {
$("#search_text").keyup(keyupfunction);
$("#search_name").keyup(keyupfunction);
});

您可以在查询中使用:

SELECT * FROM base WHERE name LIKE '".$_POST["name"]."%' AND surname LIKE '".$_POST["surname"]."%'

如果您不希望两个字段都具有值,您也可以使用 from Or

还有另一种解决方案,您可以将 2 个选择合并为 2 个不同的表

SELECT * FROM base WHERE name LIKE '".$_POST["name"]."%'
union all
SELECT * FROM base WHERE surname LIKE '".$_POST["surname"]."%'

关于php - 使用 php 将搜索中的两个单独的输入字段合并,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37236023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com