如何使用nodejs数据填充html下拉列表

时间:2016-12-14 17:43:41

标签: javascript php html node.js

我想使用NodeJS中的JSON数据填充HTML / PHP中的选择下拉菜单。我怎样才能做到这一点?当我运行localhost:3000 /到达/航班时,我得到了所有JSON航班数据,所以我知道代码是正确的,但我如何将数据从index.js传输到liveArrivalsNode.php下拉菜单?

liveArrivalsNode.php

<html>
<head>
<link rel="stylesheet" href="colour.css">
    <script src="liveArrivalsNode.js" type="text/javascript"></script>
</head>

<body>
<?php
require 'airportArrivals.html';
?>

    <form method="GET" action="arrivals/flights">
        Filter by Flight No.:
        <select class="flight" id="flights" name="flight">
            <option value="All">Any</option>

    </select>
    </form>
<p>
<div id="myFlights"></div>
</body>
<footer>
<caption align="bottom"><hr/><center>L00091898
</footer>
</html>

liveArrivalsNode.js

var xmlhttp;

window.onload=function(){           
    getAjaxData();
    setInterval(getAjaxData, 5000);

    // document.getElementById("flights").onchange=function(){
    // getAjaxData();
}

function getAjaxData() {
    if (window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

    xmlhttp.onreadystatechange=function (){
        showAjaxData();
    };

     xmlhttp.open("GET","http://localhost:3000/arrivals/",true);
     xmlhttp.send();
}

function showAjaxData()
{
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {       
        var data = JSON.parse(xmlhttp.responseText);

        var output="<table border=1>";
        output+="<tr><th></th><th>Origin</th><th>Airline</th><th>Flight</th><th>Scheduled Date</th><th>Scheduled Time</th><th>Status</th>";
        for(var i=0;i<data.arrivals.length;i++)  // for each fixture 
        {
            var terminal;
            if(data.arrivals[i].terminal == "t1")
                terminal = "<img src='images/t1.jpg'/>";
            else
                terminal = "<img src='images/t2.jpg'/>";

            output += '<tr><td>'+terminal+'</td><td>'+data.arrivals[i].origin+'</td><td>'+data.arrivals[i].airline+'</td><td>'+data.arrivals[i].flight+'</td><td>'+data.arrivals[i].scheduledDate+'</td><td>'+data.arrivals[i].scheduledTime+'</td><td>'+data.arrivals[i].status+ '<input type=\'hidden\' name=\'highlight\' value=\'data.arrivals[i].highlight\'></td></tr>';
        }
        output+="</table>";

        // add output to "fixtures" div
        document.getElementById("myFlights").innerHTML=output;
    }
}

index.js

var express = require("express");
var mysql = require('mysql');
var app = express();
var moment = require('moment');
var data;
var output;

var cors = require('cors');   // NB! Needed below.
var flightNo;
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '',
    database : 'airportdb'
});

// add route below
app.get("/arrivals/", cors(), function(req,res){

connection.query("SELECT * FROM arrivals", function(err, rows, fields) {
    if (err) throw err;             

    data = JSON.stringify(rows);

    res.writeHead(200, {"Content-Type": "text/json"});
    res.write("{ \"arrivals\":" + data+"}");
    res.end();      
    });
});

app.get("/arrivals/flights", cors(), function(req,res){
    // flightNo = req.params.flightNo;

    connection.query("SELECT flight FROM arrivals", function(err, rows, fields) {
        if (err) throw err;             

        output = JSON.stringify(rows);

        res.writeHead(200, {"Content-Type": "text/json"});
        res.write("{ \"flights\":" + output+"}");
        res.end();      
        });
});


var myServer = app.listen(3000, function() {
    soconsole.log("Server listening on port 3000");
});

0 个答案:

没有答案