当我使用form.php中的复选框选择少数位置时,我想绘制路线图。我将这些位置发布到map.php文件中,我将发布的值分配到$ location数组中,现在我想将这个$ location数组值分配到javascript calcRoute()函数的waypoints数组中。我怎么能这样做?
<form action="map.php" method="post">
<input type="checkbox" name="check_list[]" value="horton" name="wildlife"> Horton Plains National Park <br>
<input type="checkbox" name="check_list[]" value="minneriya " name="wildlife"> Minneriya National Parks <br>
<input type="checkbox" name="check_list[]" value="galoye" name="wildlife"> Galoye National park<br>
<input type="checkbox" name="check_list[]" value="udawalawa" name="wildlife"> Uda Walawe National Park<br>
<input type="checkbox" name="check_list[]" value="wasgamuwa" name="wildlife"> Wasgomuwa National Park<br>
<input type="checkbox" name="check_list[]" value="yala" name="wildlife"> Yala West (Ruhuna) National Park
</form>
<?php
$location = array();
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $selected){
$location = $selected;
}
}
}
&GT;
function calcRoute() {
var request = {
origin: 'Colombo',
destination: 'Colombo',
waypoints:[{location: 'Kandy'}, {location: 'gampaha'}, {location: 'jaffna'}],
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
答案 0 :(得分:0)
如果calcRoute()
位于map.php
中,那么您可以使用PHP将位置转换为带有$locations_json=json_encode($_POST['check_list']);
的有效JSON字符串,并使用JavaScript将其解析为全局变量或属性你之前设置的一些全局变量,如
<script type="text/JavaScript">
...
// somewhere in your code:
var locations=<?= $locations_json ?>;
...
// further JavaScript code
</script>
在JavaScript中locations
现在应该是直接的JavaScript数组,其中只包含已检查位置的名称,例如['horton','galoye','yala']
。要获得waypoints
,只需执行
waypoints=locations.map(function(e){return {location:e};});
请参阅此处查看工作示例:http://jsfiddle.net/fo4rt6vL/2/