I want to calculate the distance between two airports and return this value to jquery function and use this value in furthur calculation. i don't know where's wrong?
html: the dept and dest inputs retrieved airport information from database
<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 code:
<?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: there's nothing showing up at the airanswer i did not figure out where went wrong?
$("#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);
});
});
});
});
The data that comes from your server side script is HTML; since you're using it in Ajax, it might be a better idea to return a JSON response:
header('Content-Type: application/json');
echo json_encode(array('co2' => $co2));
Then in JavaScript:
$.post('airtravel.php',{dept: dept, dest:dest},function(data){
var dist = data.co2,
passengers = $("#numpass"),
carbon = dist * passengers;
$("#airanswer").text(carbon);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.