我正在尝试在表单中添加3个动态添加(Country-State-City)+ 2个硬编码(Type-Band)多选过滤器。选定的值将查询MySQL数据库,并将记录集发回以在OpenStreetMap上进行可视化。
所需要的例子:奥地利+瑞士在“国家”中的选择用蒂罗尔+提契诺填补“州”;选择这两个州后,“城市”将填入因斯布鲁克和卢加诺。检查最后2个过滤器中的其他多个选项将完成一组值以查询db并返回要在OSM上显示的数据。
当选择一个国家/地区城市时,网上有大量示例,我的代码可以正常运行。我无法找到多重选择的任何内容,这就是我向你寻求帮助的原因。
问题是:如上所示,如何修改我的代码以使多项选择有效?
我尝试了几种方法,但没有一种方法可以实现。
我在name =“country []”中转换了name =“country”。这适用于在单击“提交”后将值发布到php文件,但不能访问JavaScript / jQuery中的多个值。
同样,我也尝试了var country-state-city的硬编码,多合一序列化(查看代码中注明的vars)相应地修改PHP,但是我无法从所选复选框中提取值来推送将它们放入数组并准备多合一序列化。
您需要的一切如下:
<!DOCTYPE html>
<html>
<head>
<title><b>HTML</b></title>
<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src = "sumo/jquery.sumoselect.min.js"></script>
<link href = "sumo/sumoselect.css" rel="stylesheet" />
<script type = "text/javascript">
function ajaxFunction(choice) {
var httpxml;
try {
// Firefox, Opera 8.0+, Safari
httpxml = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
httpxml = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpxml = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support Ajax! Update it or change browser");
return false;
}
}
}
function stateChanged() { //callback function
if (httpxml.readyState == 4) {
if (httpxml.status == 200) {
var myObject = JSON.parse(httpxml.responseText);
document.getElementById("Json").innerHTML = httpxml.responseText;
if (choice == 's1') {
//Discard old State/Region and City options, then populate them with new options and reload SumoSelect
for (j = document.myForm.state.options.length-1; j >= 0; j--) {
document.myForm.state.remove(j);
document.myForm.city.remove(j);
}
for (i = 0; i < myObject.state.length; i++) {
var newOption = $('<option>');
newOption.attr('value', myObject.state[i]).text(myObject.state[i]);
$('#state').append(newOption);
}
$(function(){
$('#state')[0].sumo.reload();
$('#city')[0].sumo.reload();
});
}
if (choice == 's2') {
//Discard old City options while keeping State/Region, then populate with new Cities and reload SumoSelect
for (j = document.myForm.city.options.length-1; j >= 0; j--) {
document.myForm.city.remove(j);
}
for (i = 0; i < myObject.city.length; i++) {
var newOption = $('<option>');
newOption.attr('value', myObject.city[i]).text(myObject.city[i]);
$('#city').append(newOption);
}
$(function(){
$('#city')[0].sumo.reload();
});
}
} else {
alert ("An error has occurred: " + httpxml.statusText);
}
}
} //end of stateChanged() callback function
//Ajax request
var url = "ajax-dd3ck.php";
var country = myForm.country.value;
//var country = "Italy-RepubblicaSanMarino-Austria";
if (choice != 's1'){
//var state = "Piemonte-Tirol";
var state = myForm.state.value;
var city = myForm.city.value;
} else {
var state = '';
var city = '';
}
url = url + "?country=" + country;
url = url + "&state=" + state;
url = url + "&city=" + city;
url = url + "&id=" + Math.random();
document.getElementById("url").innerHTML = url;
httpxml.open("GET", url, true);
httpxml.onreadystatechange = stateChanged; //callback function
httpxml.send(null);
}
</script>
</head>
<body>
<form name = "myForm" action = "ajax-dd3-details.php" method = "post">
<select id = "country" name = "country[]" style = "width: 160px" multiple = "multiple" onchange = "ajaxFunction('s1');" >
<?php
require "config.php";
$sql = " SELECT DISTINCT Nazione FROM $db.$table ";
foreach ($dbo->query($sql) as $row) {
echo "<option value = " . $row['Nazione'] . ">" . $row['Nazione'] . "</option>";
}
?>
</select>
<script>
$('#country').SumoSelect({
placeholder: 'Select Country',
selectAll: true
});
</script>
<select id = "state" name = "state[]" style = "width: 160px" multiple = "multiple" onchange = "ajaxFunction('s2');" >
</select>
<script>
$("#state").SumoSelect({
placeholder: 'Select State/Region',
selectAll: true
});
</script>
<select id = "city" name = "city[]" style = "width: 160px" multiple = "multiple" onchange = "ajaxFunction('s3');" >
</select>
<script>
$("#city").SumoSelect({
placeholder: 'Select City',
selectAll: true
});
</script>
<select name = "type[]" class = "testselect2" style = "width: 160px" multiple = "multiple" >
<option value="3D">3D</option>
<option value="C4FM">C4FM</option>
<option value="ATV">ATV</option>
<option value="DMR">DMR</option>
<option value="DS">DS</option>
<option value="EL">EL</option>
<option value="FDMA">FDMA</option>
<option value="LN">LN</option>
<option value="Beacon">Beacon</option>
<option value="Analogic">Analogic</option>
</select>
<script>
$('.testselect2').SumoSelect({
placeholder: 'Select Type',
selectAll: true
});
</script>
<select class = "testselect2" style = "width: 160px" name = "band[]" multiple = "multiple" >
<option value="29MHz">29 MHz</option>
<option value="50MHz">50 MHz</option>
<option value="VHF">VHF</option>
<option value="UHF">UHF</option>
<option value="SHF">SHF</option>
</select>
<script>
$('.testselect2').SumoSelect({
placeholder: 'Select Band',
selectAll: true
});
</script>
<input type = "submit" value = "Submit">
</form>
<br><br><br><br><br><br>
<br><br><br><br><br><br>
<br><br><br><br><br><br>
<div id = "url"></div>
<div id = "Json"></div>
</body>
</html>
Ajax的dd3ck.php
<?Php
require "config.php";
error_reporting(0);// With this no error reporting will be there
$country = $_GET['country'];
$state1 = $_GET['state'];
$city1 = $_GET['city'];
// Validate the inputs - Checking country variable
if ((strlen($country)) > 0 and (!ctype_alpha($country))) {
echo "Data Error";
exit;
}
// Checking state variable (with space)
if ((strlen($state1)) > 0 and ctype_alpha(str_replace(' ', '', $state1)) === false) {
echo "Data Error";
exit;
}// end of input validation
if (strlen($country) > 0) {
$q_country = "SELECT DISTINCT(Regione) FROM $db.$table WHERE Nazione = '$country'";
}
$sth = $dbo->prepare($q_country);
$sth->execute();
$state = $sth->fetchAll(PDO::FETCH_COLUMN);
$q_state = "SELECT DISTINCT(Provincia) FROM $db.$table WHERE ";
if (strlen($state1) > 0) {
$q_state = $q_state . "Regione = '$state1'";
}
$sth = $dbo->prepare($q_state);
$sth->execute();
$city = $sth->fetchAll(PDO::FETCH_COLUMN);
$main = array('state'=>$state,'city'=>$city,'value'=>array("state1"=>"$state1","city1"=>"$city1"));
echo json_encode($main);
?>
MySQL:创建表格
CREATE TABLE IF NOT EXISTS `markers` (
`ID` int(4) NOT NULL AUTO_INCREMENT,
`Ripetitore` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
`Frequenza` decimal(14,6) NOT NULL,
`Shift` decimal(14,6) NOT NULL,
`Tono` decimal(10,1) DEFAULT NULL,
`Banda` int(2) DEFAULT NULL,
`ID_Nazione` int(2) NOT NULL DEFAULT '1',
`Nazione` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL DEFAULT 'Italy',
`ID_Regione` int(2) NOT NULL,
`Regione` varchar(25) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
`ID_Provincia` int(3) NOT NULL,
`Provincia` varchar(22) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
`Localita` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
`ID_Tipologia` int(2) NOT NULL,
`Tipologia` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`Identificativo` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`Rete` varchar(34) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`Locator` varchar(6) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
`Lat` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
`Long` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci NOT NULL,
`Gestore` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_mysql500_ci DEFAULT NULL,
`Data` date NOT NULL DEFAULT '0000-00-00',
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=1969 DEFAULT CHARSET=latin1;
MySQL:插入数据
INSERT INTO `markers` (`ID`, `Ripetitore`, `Frequenza`, `Shift`, `Tono`, `Banda`, `ID_Nazione`, `Nazione`, `ID_Regione`, `Regione`, `ID_Provincia`, `Provincia`, `Localita`, `ID_Tipologia`, `Tipologia`, `Identificativo`, `Rete`, `Locator`, `Lat`, `Long`, `Gestore`, `Data`) VALUES
(1, 'RV', '144.850000', '0.000000', '0.0', 15, 1, 'Italy', 0, 'ValleAosta', 102, 'Aosta', 'Aosta', 1, 'C4FM', 'IX1VKK', '', 'JN35PR', '45.73512', '7.30514', 'ix1vkk', '2016-11-29'),
(2, 'R0', '145.600000', '-0.600000', '74.4', 15, 1, 'Italy', 0, 'ValleAosta', 102, 'Aosta', 'Courmayeur (AO)', 16, 'Analogic', '', '', 'JN35LT', '45.8125', '6.958333', 'ari aosta ix1vkk', '2016-11-29'),
(3, 'R6', '145.750000', '-0.600000', '74.4', 15, 1, 'Switzerland', 0, 'Ticino', 102, 'Lugano', 'Lugano', 10, 'EL', '357734', '', 'JN35TS', '45.77513', '7.63412', 'ari aosta ix1vkk', '2016-11-29'),
(4, 'R7', '145.775000', '-0.600000', '0.0', 15, 1, 'Germany', 0, 'FreistaatBayern', 102, 'Munchen', 'Munchen', 4, 'DS', 'IX1VKK', '', 'JN35TS', '45.770833', '7.625', 'ari aosta', '2016-11-29'),
(5, 'RU12', '430.300000', '5.000000', '0.0', 20, 1, 'Slovenia', 0, 'ObalnoKraska', 102, 'Capodistria', 'Capodistria', 3, 'DMR', 'IX1VKK - 222111', 'BrandMeister', 'JN35PR', '45.72917', '7.29167', 'IX1VKK', '2017-01-21'),
(6, 'RU14', '431.950000', '0.000000', '0.0', 20, 1, 'Italy', 0, 'ValleAosta', 102, 'Aosta', 'Aosta (AO)', 1, 'C4FM', 'IX1VKK', '', 'JN35PR', '45.72917', '7.29167', 'IX1VKK - ARI Aosta', '2017-02-24'),
(7, 'RU15', '431.975000', '-1.600000', '74.4', 20, 1, 'Austria', 0, 'Tirol', 102, 'Innsbruck', 'Innsbruck', 16, 'Analogic', '', '', 'JN35PQ', '45.68280', '7.30574', 'IX1VKK - ARI Aosta', '2017-02-24'),
(8, '50 MHz', '50.480000', '0.500000', '123.0', 10, 1, 'RepubblicaSanMarino', 1, 'SanMarino', 60, 'CittaDiSanMarino', 'CittaDiSanMarino', 16, 'Analogic', '', 'Val Dora', 'JN35XM', '45.520833', '7.958333', 'iw1fwu', '2016-11-29'),
(9, 'E', '144.600000', '0.000000', '127.3', 15, 1, 'Italy', 1, 'Piemonte', 63, 'Torino', 'Torino', 10, 'EL', '983646', '', 'JN35TB', '45.0625', '7.625', 'ik1weg', '2016-11-29'),
(10, 'T', '144.625000', '0.000000', '82.5', 15, 1, 'Italy', 1, 'Piemonte', 63, 'Torino', 'Castagneto Po (TO)', 16, 'Analogic', '', 'Chivasso', 'JN35WD', '45.145833', '7.875', 'ari chivasso', '2016-11-29'),
(11, 'E', '144.700000', '0.000000', '114.8', 15, 1, 'Italy', 1, 'Piemonte', 63, 'Torino', 'Sestriere (TO)', 10, 'EL', '229829', 'Val Susa', 'JN34KW', '44.9375', '6.875', 'ik1ybm', '2016-11-29'),
(12, 'RV', '144.987500', '1.000000', '0.0', 15, 1, 'Italy', 1, 'Piemonte', 63, 'Torino', 'Torino (TO)', 4, 'DS', 'IR1CJ', ' ', 'JN35UB', '45.0625', '7.708333', 'IW1GAP', '2017-03-23'),
(13, 'E', '145.225000', '0.000000', '94.8', 15, 1, 'Italy', 1, 'Piemonte', 57, 'Verbania', 'Verbania', 10, 'EL', '573542', '', 'JN45GW', '45.9375', '8.541667', 'iz1uqg iz1ujj iz1xjr', '2016-11-29'),
(14, 'RV', '145.287500', '-0.600000', '82.5', 15, 1, 'Italy', 1, 'Piemonte', 62, 'Asti', 'Montaldo Scarampi (AT) ', 16, 'Analogic', '', '', 'JN44DT', '44.8125', '8.291667', 'iz1jvh', '2016-11-29'),
(15, 'E', '145.325000', '0.000000', '0.0', 15, 1, 'Italy', 1, 'Piemonte', 64, 'Cuneo', 'Cuneo', 10, 'EL', '391101', '', 'JN34SJ', '44.395833', '7.541667', 'sconosciuto', '2016-11-29');
有关SumoSelect的文档可以通过演示HERE
找到HERE我已经苦苦挣扎了好几天,我看不出这个问题,因为太过专注于它!任何帮助/答案/建议将不胜感激!
答案 0 :(得分:1)
根据您对问题的最佳理解,您希望通过ajax调用将多选数据发送到服务器。
首先,您需要序列化数据。 例如。要将多个状态发送到服务器,您可以执行类似
的操作state = $('#state').val()
state = JSON.stringify(state)
.....
// send state to server
url = url + "&state=" + state;
...
然后在php中你可以收到像
这样的值$state = $_GET['state'];
$states_array = json_decode($state);
var_dump($states_array);
答案 1 :(得分:0)
修改后的脚本基于Hemant的答案(为第一个过滤器编写,即国家,然后根据州和城市再次修改):
<script type = "text/javascript">
function ajaxFunction(choice) {
var httpxml;
try {
// Firefox, Opera 8.0+, Safari
httpxml = new XMLHttpRequest();
} catch (e) {
// Internet Explorer
try {
httpxml = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpxml = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("Your browser does not support Ajax! Update it or change browser");
return false;
}
}
}
function stateChanged() { //callback function
if (httpxml.readyState == 4) {
if (httpxml.status == 200) {
var myObject = JSON.parse(httpxml.responseText);
document.getElementById("Json").innerHTML = httpxml.responseText;
if (choice == 's1') {
//Discard old State/Region and City options, then populate them with new options and reload SumoSelect
for (j = document.myForm.state.options.length-1; j >= 0; j--) {
document.myForm.state.remove(j);
document.myForm.city.remove(j);
}
for (i = 0; i < myObject.state.length; i++) {
var newOption = $('<option>');
newOption.attr('value', myObject.state[i]).text(myObject.state[i]);
$('#state').append(newOption);
}
$(function(){
$('#state')[0].sumo.reload();
$('#city')[0].sumo.reload();
});
}
if (choice == 's2') {
//Discard old City options while keeping State/Region, then populate with new Cities and reload SumoSelect
for (j = document.myForm.city.options.length-1; j >= 0; j--) {
document.myForm.city.remove(j);
}
for (i = 0; i < myObject.city.length; i++) {
var newOption = $('<option>');
newOption.attr('value', myObject.city[i]).text(myObject.city[i]);
$('#city').append(newOption);
}
$(function(){
$('#city')[0].sumo.reload();
});
}
} else {
alert ("An error has occurred: " + httpxml.statusText);
}
}
} //end of stateChanged() callback function
//Ajax request
var url = "ajax-dd3ck.php";
var country = $('#country').val();
country = JSON.stringify(country);
//document.getElementById('textCountry').innerHTML = country;
if (choice != 's1'){
var state = myForm.state.value;
var city = myForm.city.value;
} else {
var state = '';
var city = '';
}
url = url + "?country=" + country;
url = url + "&state=" + state;
url = url + "&city=" + city;
url = url + "&id=" + Math.random();
document.getElementById("url").innerHTML = url;
httpxml.open("GET", url, true);
httpxml.onreadystatechange = stateChanged; //callback function
httpxml.send(null);
}
</script>
这允许我以ajax-dd3ck.php?country = [“Italy”,“RepubblicaSanMarino”]&amp; state =&amp; city =&amp; id = 0.24461933500488242的形式发送Ajax请求,以便被php消化页。 再次感谢 !希望这段代码可以帮助其他人。