我想从MySQL表中搜索和检索数据。我使用了两个文本字段来获取搜索关键字。我为此使用了jquery ajax。我第一次只使用位置进行搜索。然后它工作。当我使用两个文本字段时,它不起作用。然后我总是no data
。
<div class="container">
<form action="search.php" method="post">
<input type="text" id="location" name="location">
<input type="text" id="catogary" name="catogary">
<script>
$('#location,#catogary').keyup(function () {
var loca=$(this).val();
var cato=$(this).val();
if(loca!='' || cato!=''){
$.ajax({
url:"search.php",
method:"POST",
data:{searc:loca,cato:cato},
DataType:"text",
success:function (data) {
$('#result').html(data);
}
});
}
else{
$('#result').html('');
}
});
</script>
</form>
<div id="result"></div>
</div>
php code
<?php
$conn=mysqli_connect("localhost","root","","internship");
$output='';
$sql="select * from vacancy where location like '%".$_POST["searc"]."%'
and catogary like '%".$_POST["cato"]."%'";
$res=mysqli_query($conn,$sql);
if(mysqli_num_rows($res)>0){
while($row=mysqli_fetch_assoc($res)){
?>
<div class="bs-calltoaction bs-calltoaction-primary">
<div class="row">
<div class="col-md-9 cta-contents">
<h1 class="cta-title">Its a Call To Action</h1>
<div class="cta-desc">
<input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br>
<input type="text" value='<?= $row['company_name'];?>' readonly style="width: 75%"><br><br>
<input type="text" value='<?= $row['location'];?>' readonly style="width: 75%"><br><br>
<input type="text" value='<?= $row['qulification'];?>' readonly style="width: 75%"><br><br>
<input type="text" value='<?= $row['catogary'];?>' readonly style="width: 75%"><br><br>
<input type="text" value='<?= $row['indate'];?>' readonly style="width: 37.5%">
<input type="text" value='<?= $row['expdate'];?>' readonly style="width: 37.5%">
</div>
</div>
<div class="col-md-3 cta-button">
<a class="btn btn-primary btn-large"
href="#myModal" data-toggle="modal">Apply for job</a>
</div>
</div>
</div>
<?php
}
}
else{
echo 'no data';
}
?>
答案 0 :(得分:2)
更改以下2行,
var loca=$(this).val();
var cato=$(this).val();
通过
var loca=$('#location').val();
var cato=$('#catogary').val();
在$.ajax()中使用dataType
而不是DataType
,
$.ajax({
url:"search.php",
method:"POST",
data:{searc:loca,cato:cato},
dataType:"text", // use dataType not DataType
success:function (data) {
$('#result').html(data);
}
});
答案 1 :(得分:0)
使用 var loca = $(&#39; #position&#39;)。val(); var cato = $(&#39; #catogary&#39;)。val();
获取输入值
使用此查询
$sql="select * from vacancy";
$where = "";
if(!empty($_POST["searc"]){
if($where == "") {
$where = $where." where location like '%".$_POST["searc"]."%'";
} else {
$where = $where." AND location like '%".$_POST["searc"]."%'";
}
}
if(!empty($_POST["cato"]){
if($where == "") {
$where = $where." where catogary like '%".$_POST["cato"]."%'";
} else {
$where = $where." AND catogary like '%".$_POST["cato"]."%'";
}
}
$sql = $sql.$where;