Есть 2 select, которые должны фильтровать статьи из БД через ajax(версии php и ajax старые).
Option берутся из 2 разных таблиц. Выбрав 2 нужных option выводятся статьи из третьей таблицы. Я написал все запросы и скрипт вроде подкорректировал, но что-то не так.
Почему не работает?
index.php
<div>
<select id="get_country" name="get_country">
<!-- запрос в бд для вывода станций в первом select -->
<option value="">Выберите Метро</option>
<?php
$result = mysql_query("SELECT * FROM station");
while ($row = mysql_fetch_array($result)){
echo "<option value='".$row["id"]."'>".$row["name"]."</option>";
}
?>
</select>
</div>
<div id="sub_region">
<select id="get_region" name="get_region">
<!-- запрос в бд для вывода квартир во вотором select -->
<?php
$result = mysql_query("SELECT * FROM flats");
echo"<option value=''>выберите Квартиру</option>";
while ($row = mysql_fetch_array($result)){
echo "<option value='".$row["id"]."'>".$row["name"]."</option>";
}?>
</select>
</div>
<div id="sub_city" class="item">
<div id="get_city" name="get_city">
<?php
// изначальный вывод всех статей в случайном порядке
$result = mysql_query("SELECT * FROM order_post ORDER BY rand()");
while ($row = mysql_fetch_array($result))
{
echo "<div class='infoFlat'>";
echo "<h3 value='".$row["id"]."'>".$row["metro"]."</h3>";
echo "<h3 value='".$row["id"]."'>".$row["room"]."</h3>";
echo "<p value='".$row["id"]."'>".$row["texts"]."</p>";
echo "<p value='".$row["id"]."'>".$row["price"]."</p>";
echo "</div>";
}
?>
</div>
</div>
Файл, что отвечает за запрос в бд от двух совместных select
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$result = mysql_query("SELECT * FROM order_post WHERE metroid = '".$_POST["station"]."' AND metroid = '".$_POST["station"]."'");
while ($row = mysql_fetch_array($result)) {
echo "<div class='infoFlat'>";
echo "<h3 value='".$row["id"]."'>".$row["metro"]."</h3>";
echo "<h3 value='".$row["id"]."'>".$row["flat"]."</h3>";
echo "<p value='".$row["id"]."'>".$row["texts"]."</p>";
echo "<p value='".$row["id"]."'>".$row["price"]."</p>";
echo "</div>";}}
region.php
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$result = mysql_query("SELECT * FROM flats ");
echo"<option value=''>выберите Квартиру</option>";
while ($row = mysql_fetch_array($result)){
echo "<option value='".$row["id"]."'>".$row["name"]."</option>";
}}
скрипт
$(document).ready(function () {
$('#sub_region').css('display', 'none');
// $('#sub_city').css('display', 'none');
$("#get_country").change(function() {
clearlist();
$('#sub_city').css('display', 'none');
var countryvalue = $("#get_country option:selected").val();
if (countryvalue === '') {clearlist(); }
if (countryvalue === '') {clearlist(); $('#sub_region').css('display', 'none'); }
getarea();
})
$("#get_region").change(function() {
clearlist();
$('#sub_city').css('display', 'none');
var countryvalues = $("#get_region option:selected").val();
if (countryvalues === '') {clearlist(); }
if (countryvalues === '') {clearlist(); $('#sub_region').css('display', 'none'); }
getarea();
})
getarea();
getcity();
function getarea() {
var country_value = $("#get_country option:selected").val();
var regions_value = $("#get_region option:selected").val();
var p_id = $("#page_id").val();
var area = $("#get_city");
var getarea_value = area.val();
var p_reg_id = $("#page_id").val();
var area_region = $("#get_region");
var getareas_value = area_region.val();
if (country_value === "" && regions_value === country_value) {
area.attr("disabled",true);
area_region.attr("disabled",true);
} else {
area.attr("disabled",false);
area_region.attr("disabled",false);
area.load('get_city.php',{country : country_value, page_id : p_id});
area_region.load('get_region.php',{regions : regions_value, page_id : p_reg_id});
$('#sub_city').css('display', 'block');
}
$("#get_city").change(function() {
getcity();
})
}
function getcity() {
var country_value = $("#metroid").val();
var country_value = $("#get_city option:selected").val();
var regions_value = $("#flatid").val();
var regions_value = $("#get_city option:selected").val();
if((country_value == undefined) || (regions_value == "")){
var country_value = $("#metroid").val();
var regions_value = $("#flatid").val();
}
var p_id = $("#page_id").val();
var area = $("#get_region");
var p_reg_id = $("#page_id").val();
var area_region = $("#get_region");
if ((country_value === "") || (regions_value === "")) {
area.attr("disabled",true);
area_region.attr("disabled", true);
$('#sub_region').css('display', 'none');
$("#get_region").empty();
$('#sub_city').css('display', 'none');
$("#get_city").empty();
} else {
area.attr("disabled",false);
area.load('get_region.php',{region : country_value, page_id : p_id});
area_region.attr("disabled",false);
area_region.load('get_region.php',{regions : regions_value, page_id : p_reg_id});
$('#sub_region').css('display', 'block');
}}
function clearlist() {
$("#get_region").empty();
$("#get_city").empty();
}});