根据两个选择选项显示mysql的结果数

时间:2016-12-26 20:47:09

标签: javascript php html mysql ajax

我有以下HTML代码,允许我从选择下拉选项中选择两项; 现在,在选择了两个项目之后,比如我选择住宿作为我的表格,坎帕拉作为我的区域,我想要可用的可能结果总数,例如,“找到25个结果”,自动显示。

我遇到的问题是只显示一个选择的结果,而另一个显示为未定义的通知。请帮助。

<!-- here is the html script for selecting options -->    

<form id="analysis_formId" method="POST">

<label>select form</label>
<select id="selectformId" name="selectform" class="select_elements">
<option value="afc">accommodation form</option>
<option value="rtt">tour and travel form</option>   
</select>

<label>select district</label>

<select id="selectdistrictsId" name="selectdistricts" class="select_elements">
<option value="alldistricts">All districts</option>
<option value="kampala">kampala</option>
<option value="wakiso">wakiso</option>   
</select>        
</form>

<!-- displaying results from count.php -->
<p><span id="inspection_result"></span></p>

这个脚本获取select元素的类名并运行onChange事件。

<script>
var selections = document.getElementsByClassName("select_elements");
function onchangefunction(){
var selectedstring = (this.options[this.selectedIndex].value);
$("#inspection_result").html('checking...');                                            
$.ajax({
type:'POST',
url:'count.php',
data:$(this).serialize(),
success:function(data)
{
$("#inspection_result").html(data);
}
});
}

for (var i = 0, l = selections.length; i < l; i++) {
selections[i].onchange = onchangefunction;
}
</script>

以下是 count.php ,用于查询MySQL

的结果
if($_POST) 
{
$formtype     = strip_tags($_POST['selectform']);
$selectdistricts     = strip_tags($_POST['selectdistricts']);


$fetchallforms22 = $conn->prepare("select count(indexId) as numafcrtt2 
from registered_companies where company_form_type=:formtype and company_district=:selectdistricts");

$fetchallforms22->execute(array(':formtype'=>$formtype, ':selectdistricts'=>$selectdistricts));

$display22 = $fetchallforms22->fetchObject();           

if($display22){

echo $display22->numafcrtt2." "."results available";
}
else
{
echo $display22->numafcrtt2." "."results available";
}
}//POST

1 个答案:

答案 0 :(得分:0)

这个查询可能没有结果,但是:

if (!empty(display2) ){
..... // DO something
}