我想计算两个机场之间的距离并将此值返回到jquery函数,并在此计算中使用此值。 我不知道哪里错了?
html:dept和dest输入从数据库中检索机场信息
<form id="airform1" class="on">
<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" placeholder="City name or airport code" >
</div>
</div>
<div>
<div>
<input type="radio" name="trip" value="2.0">Round-trip <br>
<input type="radio" name="trip" value="1.0">One-way
</div>
</div>
<div>
Number of flights/passengers
<input type="text" id="numpass" value="1">
</div>
<div>
Class
<select style="width: 82px">
<option selected="selected" value="1.0">Economy</option>
<option value="2.04">Business</option>
<option value="2.69">First</option>
</select>
</div>
<div id="line">
</div>
</form>
php代码:
<?php
if(isset($_POST['dept'], $_POST['dest'])){
$dept=$_POST['dept'];
$dest=$_POST['dest'];
}
mysql_connect("localhost","abv","asdasda") or die (mysql_error());
mysql_select_db("asda") or die(mysql_error());
$strSQL1 = "SELECT display, lat, longi FROM airport WHERE display = '$dept'";
$rs1 = mysql_query($strSQL1);
$row1 = mysql_fetch_array($rs1);
$lat1= $row1['lat'];
$long1= $row['longi'];
$strSQL2 = "SELECT display, lat, longi FROM airport WHERE display = '$dest'";
$rs2 = mysql_query($strSQL2);
$row2 = mysql_fetch_array($rs2);
$lat2= $row1['lat'];
$long2= $row['longi'];
$earthradius = 6366.707;
$km_to_miles = 1/1.609344;
$radlat1 = M_PI * (lat1)/180;
$radlat2 = M_PI * (lat2)/180;
$radlng1 = M_PI * (long1)/180;
$radlng2 = M_PI * (long2)/180;
$radlng1 += M_PI / 2;
$radlng2 += M_PI / 2;
if($radlng1<0)
radlng1 += M_PI * 2;
if($radlng2<0)
radlng2 += M_PI * 2;
$x1 = $earthradius * cos($radlng1) * sin($radlat1);
$y1 = $earthradius * sin($radlng1) * sin($radlat1);
$z1 = $earthradius * cos($radlat1);
$x2 = $earthradius * cos($radlng2) * sin($radlat2);
$y2 = $earthradius * sin($radlng2) * sin($radlat2);
$z2 = $earthradius * cos($radlat2);
$d = sqrt(($x1-$x2)*($x1-$x2)+($y1-$y2)*($y1-$y2)+($z1-$z2)*($z1-$z2));
$theta = acos(($earthradius*$earthradius + $earthradius * $earthradius - $d * $d)/(2 * $earthradius * $earthradius));
$distance = $theta * $earthradius;
$miles = $distance * $km_to_miles;
$co2 = (($miles / 41.986) * 20.88 * 2.7) / 2204.6;
echo $co2;
?>
javascript:airanswer上没有任何东西出现我没想出哪里出错了?
$("#dept").change(function(){
$("#dest").change(function(){
var dept = $("#dept").val();
var dest = $("#dest").val();
$.post('airtravel.php',{dept: dept, dest: dest}, function(data){
$("#aircalc").click(function(){
$("#airanswer").html(data);
});
});
});
});
答案 0 :(得分:0)
来自服务器端脚本的数据是HTML;因为你在Ajax中使用它,所以返回JSON响应可能是个更好的主意:
header('Content-Type: application/json');
echo json_encode(array('co2' => $co2));
然后在JavaScript中:
$.post('airtravel.php',{dept: dept, dest:dest},function(data){
var dist = data.co2,
passengers = $("#numpass"),
carbon = dist * passengers;
$("#airanswer").text(carbon);
});