[英]if the text input field is optional, how should i pass the value of it from javascript to php
I am really new to web development. 我真的是Web开发的新手。 I want to calculate the air travel distance between two airports.
我想计算两个机场之间的空中旅行距离。 I have input fields for "from" and "to" to filled in and connected to the database.
我有输入字段,用于“从”和“到”来填写并连接到数据库。 Then i can retrieve the latitude and longitude for calculation.
然后,我可以检索经度和纬度进行计算。 I have another field for the stop during the flight.
在飞行中,我还有另一个要停的地方。 The flight could be non-stop or it transfer via somewhere.
航班可以是不间断的,也可以通过某个地方转移。 How should i pass the 'via' field in jquery to php.
我应该如何将jquery中的“ via”字段传递给php。 Sometimes the 'Via' field will not be activated and sometimes it will get a value.
有时'Via'字段不会被激活,有时会得到一个值。 There is something wrong with the following code, because $via.val() is not guaranteed.
以下代码有问题,因为不能保证$ via.val()。 how should i fix this ?
我该如何解决? is there any problem with my php code also ?
我的php代码也有问题吗?
$.post('airtravel1.php',{dept: $dept.val(), dest: $dest.val(), via: $via.val()}, function(data){
This is my html part: 这是我的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>
This is javascript, jquery: var $dept = $("#dept"); 这是javascript,jquery:var $ dept = $(“#dept”); var $dest = $("#dest");
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();
}
});
The php: 的PHP:
if(isset($_POST['dept'], $_POST['dest'])){
$dept=$_POST['dept'];
$dest=$_POST['dest'];
}
if(isset($_POST['via'])){
$via=$_POST['via'];
indirect($dept, $dest, $via);
}else{
direct($dept, $dest);
}
function direct($dept, $dest){
$strSQL1 = "SELECT display, lat, longi FROM airport WHERE display = '$dept'";
$rs1 = $mysqli->query($strSQL1);
$row1 = $rs1->fetch_assoc();
$lat1= $row1['lat'];
$long1= $row1['longi'];
$strSQL2 = "SELECT display, lat, longi FROM airport WHERE display = '$dest'";
$rs2 = $mysqli->query($strSQL2);
$row2 = $rs2->fetch_assoc();
$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;
echo json_encode(array('co2' => $co2,'miles'=>$miles));
}
You can check this in php with the function empty()
. 您可以在PHP中使用函数
empty()
。
if(isset($_POST['via']) && !empty($_POST['via'])){
$via=$_POST['via'];
indirect($dept, $dest, $via);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.