从表单获取输入以在JSON中使用

时间:2017-12-04 05:42:38

标签: javascript jquery json ajax

所以,当涉及到混淆JSON,AJAX和那种东西时,我是新手...... 我有这个任务,我应该有4个输入字段:&#34;开始城市&#34;,&#34;开始状态&#34;,&#34;目标城市&#34;和&#34;目的地状态&#34;。 它基本上应该得到城市A到城市B之间的距离。它被赋予了一个文件,其背后的信息,我需要做的是获取输入,并从文件中获取数据,如果这是有道理的。< / p>

我想过有一个像这样的javascript函数:

function getData()
{
    var xhttpData = new XMLHttpRequest();
    var url = "http://localhost/cgi-
        bin/ercanbracks/mileage/mileageAjaxJSON";

    xhttpData.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            var parseData = this.responseText;
            console.log(parseData);
            var trip = parseData.trip;
            document.getElementsByName("startCity")[0].value =  
                trip.startcity;
            document.getElementsByName("startState")[0].value =  
                trip.startstate;
            document.getElementsByName("endCity")[0].value =  trip.endcity;
            document.getElementsByName("endState")[0].value =  
                trip.endstate;

            document.getElementById("demo").innerHTML =  trip.startcity + ", 
                " + trip.startstate + ", " + trip.miles + " miles to " + 
                trip.endcity + ", " + trip.endstate + ".";
        }
    };


    xhttpData.open("GET", url, true);
    xhttpData.send();
}
document.addEventListener("DOMContentLoaded", function(event) { getData(); 
});

以下是表格:

<!DOCTYPE html>
<html>
    <head>
        <script type="text/javascript" 
            src="http://localhost/~arthurfig/week11.js"></script>
        <title></title>

    </head>
    <body>
        <form action="" onsubmit="getData()">
            <p>Start City: </p>
            <input type="text" name="startCity">
            <p>Start State: </p>
            <input type="text" name="startState">
            <p>Destination City: </p>
            <input type="text" name="endCity">
            <p>Destination State: </p>
            <input type="text" name="endState">
            <input type="submit" value="Check">
        </form>
        <p id="demo"></p>
    </body>
</html>

以下是文件中的内容:

{"trip" : 
  { "startcity" : "",
    "startstate" : "",
    "endcity" : "",
    "endstate" : "",
    "miles" : "Unknown"
  }
}

我的问题是,我不确定如何从表单中获取文本字段的值以设置为变量的值,并使用这些值获取距离(英里)。

2 个答案:

答案 0 :(得分:0)

试试这个:

function getData() {
    var xhttpData = new XMLHttpRequest();
    var url = "http://localhost/cgi-bin/ercanbracks/mileage/mileageAjaxJSON";

    xhttpData.onreadystatechange = function () {
        if (this.readyState == 4 && this.status == 200) {
            var parseData = JSON.parse(this.responseText);
            var trip = parseData.trip;
            document.getElementsByName("startCity")[0].value =  trip.startcity;
            document.getElementsByName("startState")[0].value =  trip.startstate;
            document.getElementsByName("endCity")[0].value =  trip.endcity;
            document.getElementsByName("endState")[0].value =  trip.endstate;

            var text = trip.startcity + ", " trip.startstate + ", " + trip.miles + " miles to " + trip.endcity + ", " + trip.endstate + ".";
            document.getElementById("demo").innerHTML = text;
        }
    };


    xhttpData.open("GET", url, true);
    xhttpData.send();
}

答案 1 :(得分:0)

BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE band cascade constraints';
EXCEPTION
   WHEN OTHERS THEN
      IF SQLCODE <> -942 THEN
         RAISE;
      END IF;
END;
CREATE TABLE band..