我有一个实时搜索设置,并且可以正常使用on post值。我想向我的DB查询添加一个参数,该参数涉及额外的";张贴";价值这是使用php和ajax。
变量$school显示在文本字段中,并由$_GET['sch']值填充。执行ajax时,查询显示为空。(使用echo$Query(
当";"回响";,&查询如下:
SELECT*FROM tears WHERE FullName like%test%and shcool_id=''
这是PHP
<?php
if (isset($_POST['search'])) {
$school = $_GET['sch'];
$Name = $_POST['search'];
$Query = "SELECT * FROM teachers WHERE fullName LIKE '%$Name%' AND school_id='$school' ";
$ExecQuery = MySQLi_query($conn, $Query);
echo '<div class="list-group" style="text-align:left;">';
while($Result = MySQLi_fetch_array($ExecQuery)) {
?>
<a href="?id=<?php echo $Result['teacher_id']; ?>&sch=<?php echo $Result['school_id']; ?>" type="button" width="100%" onclick='fill("<?php echo $Result['fullName']; ?>")' class="list-group-item list-group-item-action">
<?php echo $Result['fullName']; ?>
</a>
<?php
}
}
$conn->close();
?>
</div>
这是脚本文件:
function fill(Value) {
$('#search').val(Value);
$('#display').hide();
}
$(document).ready(function() {
$("#search").keyup(function() {
var name = $('#search').val();
if (name == "") {
$("#display").html("");
}
else {
$.ajax({
type: "POST",
url: "ajax.php",
data: {
search: name
},
success: function(html) {
$("#display").html(html).show();
}
});
}
});
});
这是表格:
<div class="input-group" style="margin-top: 5.5em;">
<button class="btn btn-outline-primary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false">Search By</button>
<ul class="dropdown-menu">
<li><a class="dropdown-item" href="/isbn/title/?sch=<?php echo $_GET['sch']; ?>">Book Title</a></li>
<li><a class="dropdown-item" href="/isbn/author/?sch=<?php echo $_GET['sch']; ?>">Author</a></li>
<li><a class="dropdown-item" href="/isbn/teacher/?sch=<?php echo $_GET['sch']; ?>">Teacher</a></li>
</ul>
<input placeholder="<?php echo $placeholder; ?>" class="form-control" aria-label="Text input with dropdown button"
onblur="this.focus()" onfocus="this.value=''" type="text" id="search" autocomplete="off" <?php echo $disabled; ?> />
<input class="form-control" type="text" id="school" value="<?php echo $_GET['sch']; ?>" />
<br>
<div id="display" style="width: 100%;"></div>
</div>
我试图分配数据。我试着添加var=学校等我已经尝试在PHP页面上直接添加GET值我可以看到该值被填充在id="0"中;学校;文本框。
data: {
search: name, school: school
},
到ajax文件
在ajax调用之前获取查询参数。
let sch = '';
let queryParms = new URLSearchParams(window.location.search);
if (queryParms) {
sch = queryParms.get('sch');
}
...
$.ajax({
type: "POST",
url: "ajax.php",
data: {
search: name,
sch: queryParms.get('sch')
},
success: function(html) {
$("#display").html(html).show();
}
});
在后端使用并检查非空值。
$_POST['sch'];