我是网络开发的新手。我想计算两个机场之间的旅行距离。 如果通过填充,那么城市将计算为总距离。 如果没有填充,我仍然可以计算不间断的航空旅行距离,但我怎样才能将该值传递给javascript中的php文件。 我认为我的代码在javascript部分有问题 此外,这个名单($ co2,$ miles)是json吗?我可以用javascript解析这个列表吗?
这是html部分:
<div>
From
<div class="textinput">
<input type="text" id="dept" name="departure" placeholder="City name or aiport code" >
</div>
</div>
<div>
To
<div>
<input type="text" id="dest" placeholder="City name or airport code" >
</div>
</div>
<div>
Via
<div>
<input type="text" id="via" value="0" placeholder="City name or airport code" >
</div>
</div>
这是javascript部分
var $dept = $("#dept");
var $dest = $("#dest");
var $via = $("#via");
$("#aircalc").on('click', function() {
if($("#airradio1").is(':checked')) {
$("#airanswer").val("");
$.post('airtravel1.php',{dept: $dept.val(), dest: $dest.val(), via: $via.val()}, function(data) {
var response = jQuery.parseJSON(data);
var a = response.co2;
var mile = (response.miles).toFixed(4);
var numpass = $("#numpass").val();
var flightclass = $("#flightclass").val();
var trip = $("input[name='trip']:checked").val();
var total = (a * trip * flightclass * numpass).toFixed(2);
var sum = "<div>Trip from " + $dept.val() + " to " + $dest.val() + ", you traveled " + mile + "miles</div>";
$("#airanswer").text(total);
$("#airresult").on('click',function(){
$("#results").append(sum);
});
});
} else {
myairFunction2();
}
});
php部分:
<?php
if(isset($_POST['dept'], $_POST['dest'])) {
$dept=$_POST['dept'];
$dest=$_POST['dest'];
}
mysql_connect("localhost","ccc","aaa") or die (mysql_error());
mysql_select_db("ccc") or die(mysql_error());
if(isset($_POST['via'])) {
$via=$_POST['via'];
list($co2, $miles) = indirect($dept, $dest, $via);
} else {
list($co2, $miles) = direct($dept, $dest);
}
function direct($dept, $dest) {
$strSQL1 = "SELECT display, lat, longi FROM airport WHERE display = '$dept'";
$rs1 = mysql_query($strSQL1);
$row1 = mysql_fetch_array($rs1);
$lat1= $row1['lat'];
$long1= $row1['longi'];
$strSQL2 = "SELECT display, lat, longi FROM airport WHERE display = '$dest'";
$rs2 = mysql_query($strSQL2);
$row2 = mysql_fetch_array($rs2);
$lat2= $row2['lat'];
$long2= $row2['longi'];
$earthradius = 6366.707;
$km_to_miles = 1/1.609344;
$dlat = ($lat2-$lat1) * (M_PI / 180);
$dlon = ($long2-$long1) * (M_PI / 180);
$a = sin($dlat / 2) * sin($dlat / 2) + sin($dlon / 2) * sin($dlon / 2) * cos($lat1 * (M_PI / 180)) * cos($lat2 * (M_PI / 180));
$c = 2 * atan2(sqrt($a), sqrt(1-$a));
$d = $c * $earthradius;
$miles = $d * $km_to_miles;
$co2 = (($miles / 41.986) * 20.88 * 1.9) / 2204.6;
return [$co2, $miles];
}
function indirect($dept, $dest, $via) {
$strSQL1 = "SELECT display, lat, longi FROM airport WHERE display = '$dept'";
$rs1 = mysql_query($strSQL1);
$row1 = mysql_fetch_array($rs1);
$lat1= $row1['lat'];
$long1= $row1['longi'];
$strSQL2 = "SELECT display, lat, longi FROM airport WHERE display = '$dest'";
$rs2 = mysql_query($strSQL2);
$row2 = mysql_fetch_array($rs2);
$lat2= $row2['lat'];
$long2= $row2['longi'];
$strSQL3 = "SELECT display, lat, longi FROM airport WHERE display = '$via'";
$rs3 = mysql_query($strSQL3);
$row3 = mysql_fetch_array($rs3);
$lat3= $row3['lat'];
$long3= $row3['longi'];
$earthradius = 6366.707;
$km_to_miles = 1/1.609344;
$dlat1 = ($lat3-$lat1) * (M_PI / 180);
$dlon1 = ($long3-$long1) * (M_PI / 180);
$a1 = sin($dlat1 / 2) * sin($dlat1 / 2) + sin($dlon1 / 2) * sin($dlon1 / 2) * cos($lat1 * (M_PI / 180)) * cos($lat3 * (M_PI / 180));
$c1 = 2 * atan2(sqrt($a1), sqrt(1-$a1));
$d1 = $c1 * $earthradius;
$miles1 = $d1 * $km_to_miles;
$dlat2 = ($lat3-$lat2) * (M_PI / 180);
$dlon2 = ($long3-$long2) * (M_PI / 180);
$a2 = sin($dlat2 / 2) * sin($dlat2 / 2) + sin($dlon2 / 2) * sin($dlon2 / 2) * cos($lat2 * (M_PI / 180)) * cos($lat3 * (M_PI / 180));
$c2 = 2 * atan2(sqrt($a2), sqrt(1-$a2));
$d2 = $c2 * $earthradius;
$miles2 = $d2 * $km_to_miles;
$miles = $miles1 + $miles2;
$co2 = (($miles / 41.986) * 20.88 * 1.9) / 2204.6;
return [$co2, $miles];
}
?>
答案 0 :(得分:-1)
像这样更改您的Javascript代码
var data={dept: $dept.val(), dest: $dest.val(), via: $via.val()};
$.ajax({type:'POST',url:'airtravel1.php',data:data,success:function(data)
{
//your call back
console.log(data)
}});