douzhang1115 2014-03-25 16:22
浏览 33
已采纳

如果文本输入字段是可选的,我应该如何将它的值从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));

    }
  • 写回答

1条回答 默认 最新

  • dongxun1244 2014-03-25 16:31
    关注

    You can check this in php with the function empty().

    if(isset($_POST['via']) && !empty($_POST['via'])){
        $via=$_POST['via'];
        indirect($dept, $dest, $via);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况