Нашел форму выборки города. Никак не соображу как вставить сюда ещё пару таких же полей, по такому же принципу, как тут - страна-область-город.
Пытался всё повторить, как тут в обоих файлах, но видимо, что-то недосмотрел, может с закрывающими скобками запутался или вообще не то делал .
Далее нужны поля, допустим, фрукты-яблоко.
Кто силен в этих вещах? Помогите, пожалуйста!
Вот первый оригинальный код:
HTML5 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
| <script language="javascript" src="http://cite/jquery/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$.post("http://cite/cities.php", {},
function (xml) {
$(xml).find('country').each(function() {
id = $(this).find('id_country').text();
$("#country").append("<option value='" + id + "'>" + $(this).find('country_name_ru').text() + "</option>");
});
});
$("#country").change(function() {
id_country = $("#country option:selected").val();
if (id_country == "") {
$(".region, .city, .submit").hide();
}else {
$("#region").html('');
$("#region").html('<option value="">Выберите регион</option>');
$.post("http://cite/cities.php", {id_country: id_country},
function (xml) {
$(xml).find('region').each(function() {
id = $(this).find('id_region').text();
$("#region").append("<option value='" + id + "'>" + $(this).find('region_name_ru').text() + "</option>");
});
});
$(".region").show();
}
});
$("#region").change(function() {
id_region = $("#region option:selected").val();
if (id_region == "") {
$(".city").hide();
}else {
$("#city").html('');
$("#city").html('<option value="">Выберите город</option>');
$.post("http://cite/cities.php", {id_region: id_region},
function (xml) {
$(xml).find('city').each(function() {
id = $(this).find('id_city').text();
$("#city").append("<option value='" + id + "'>" + $(this).find('city_name_ru').text() + "</option>");
});
});
}
$(".city").show();
});
$("#city").change(function() {
if ($("#city option:selected").val() == "") {
$(".submit").hide();
}else {
$(".submit").show();
}
});
});
</script>
<body>
<form action="http://cite/autodrom" method="POST" enctype="multipart/form-data">
<div>
<strong>Страна:</strong>
<select name="country" id="country">
<option value="" selected>Выберите страну</option>
</select>
</div>
<div class="region">
<strong>Регион:</strong>
<select name="region" id="region">
<option value="">Выберите регион</option>
</select>
</div>
<div class="city">
<strong>Городишко:</strong>
<select name="city" id="city">
<option value="">Выберите город</option>
</select>
</div>
<div>
<input type="submit" class="submit" value="Сохранить">
</div>
</form>
</body> |
|
Вот обработчик:
PHP |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
| <?php
header('Content-type: text/xml; charset=windows-1251');
$db_host = 'localhost';
$db_user = 'user';
$db_pass = 'pass';
$db_name = 'base';
$cities = new cities($db_host, $db_user, $db_pass, $db_name);
if (isset($_POST['id_country'])) {
$cities->get_regions($_POST['id_country']);
}elseif (isset($_POST['id_region'])) {
$cities->get_cities($_POST['id_region']);
}
else {
$cities->get_countries();
}
class cities {
private $db;
public $xml = '<?xml version="1.0" encoding="windows-1251"?><root>';
function __construct($db_host, $db_user, $db_pass, $db_name) {
$this->db = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $this->db);
}
function get_countries() {
$mysql_result = mysql_query("SELECT * FROM countries ORDER BY country_order", $this->db);
while($country = mysql_fetch_assoc($mysql_result)) {
$this->xml .= '<country>';
$this->xml .= '<id_country>'.$country['id_country'].'</id_country>';
$this->xml .= '<country_name_ru><![CDATA['.$country['country_name_ru'].']]></country_name_ru>';
$this->xml .= '<country_name_en><![CDATA['.$country['country_name_en'].']]></country_name_en>';
$this->xml .= '<iso>'.$country['country_iso'].'</iso>';
$this->xml .= '</country>';
}
$this->print_xml();
}
function get_regions($id_country) {
$mysql_result = mysql_query("SELECT * FROM regions WHERE id_country = ".intval($id_country)." ORDER BY region_order", $this->db);
while($region = mysql_fetch_assoc($mysql_result)) {
$this->xml .= '<region>';
$this->xml .= '<id_region>'.$region['id_region'].'</id_region>';
$this->xml .= '<region_name_ru><![CDATA['.$region['region_name_ru'].']]></region_name_ru>';
$this->xml .= '<region_name_en><![CDATA['.$region['region_name_en'].']]></region_name_en>';
$this->xml .= '</region>';
}
$this->print_xml();
}
function get_cities($id_region) {
$mysql_result = mysql_query("SELECT * FROM cities WHERE id_region = ".intval($id_region)." ORDER BY city_order", $this->db);
while($city = mysql_fetch_assoc($mysql_result)) {
$this->xml .= '<city>';
$this->xml .= '<id_city>'.$city['id_city'].'</id_city>';
$this->xml .= '<city_name_ru><![CDATA['.$city['city_name_ru'].']]></city_name_ru>';
$this->xml .= '<city_name_en><![CDATA['.$city['city_name_en'].']]></city_name_en>';
$this->xml .= '</city>';
}
$this->print_xml();
}
function print_xml() {
$this->xml .= '</root>';
echo $this->xml;
}
} |
|