如果文本输入字段是可选的,我应该如何将它的值从javascript传递给php

时间:2014-03-25 16:23:00

标签: javascript php jquery

我是网络开发的新手。我想计算两个机场之间的航空旅行距离。我有“from”和“to”的输入字段填写并连接到数据库。然后我可以检索经度和经度进行计算。我在飞行途中有另一个停靠场。航班可以不间断或通过某个地方转移。 我应该如何将jquery中的'via'字段传递给php。有时“Via”字段不会被激活,有时它会获得一个值。 以下代码有问题,因为无法保证$ via.val()。我应该怎么解决这个问题?我的PHP代码也有问题吗?

 $.post('airtravel1.php',{dept: $dept.val(), dest: $dest.val(), via: $via.val()},      function(data){

这是我的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,jquery:         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:

 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));

    }

1 个答案:

答案 0 :(得分:3)

您可以在php中使用函数empty()进行检查。

if(isset($_POST['via']) && !empty($_POST['via'])){
    $via=$_POST['via'];
    indirect($dept, $dest, $via);

}