简体   繁体   English

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

[英]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.

相关问题 如何将按钮的值传递给输入文本字段,但使用javascript在文本字段中值应消失 - How to pass the value of button to input text field but value should be disappear in text field using javascript 将变量从URL传递到PHP并传递给Javascript,并在输入字段中显示值 - Pass variable from URL to PHP to Javascript and Display Value in Input Field 如何将值从输入字段传递给javascript闭包 - how to pass value to javascript closure from input field 如何通过onchange函数将带有选项值的输入类型的PHP值传递给Javascript? - How can I pass PHP value of an input type from onchange function with the option value to Javascript? 如何将JavaScript值传递到输入字段 - How to pass the JavaScript value into input field 如何通过javascript将值传递给输入字段 - How to Pass a value to input field through javascript 将值从按钮传递到JavaScript中的文本字段 - Pass a Value from Button to Text Field in JavaScript 如何使用 JavaScript 获取文本输入字段的值? - How do I get the value of text input field using JavaScript? 如何从输入字段捕获文本值,并将其传递到新的输入字段 - How to capture text value from input field, and pass it to new input field 如何将文本字段值传递给Javascript中的变量 - How to Pass the Text field value to a variable in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM