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

I am really new to web development. 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. Sometimes the 'Via' field will not be activated and sometimes it will get a value. There is something wrong with the following code, because $via.val() is not guaranteed. how should i fix this ? is there any problem with my php code also ?

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

This is my html part:

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

 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().

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

}