0

我正在使用 MySQL Db 在 nodejs 中创建一个 Web 应用程序。我想将用户输入值传递给 app.js 文件,在该文件中使用该值在数据库中进行搜索并在另一个页面中显示结果。

这是我在 index.html 中使用的表单:

<form method="get" action="SearchResults">
<input type="text" name="zipcode" placeholder="Search with ZipCode/City..."
 required style = "width: 830px; height:20px">
<input type="submit"  value="Submit"></center>
</form>

我知道如何使用 JSP 和 Servlet 进行 SearchResults 调用。但我无法理解如何使用 Nodejs 和 MySQL 进行此调用 n Bluemix。在做了一些研究并试用了入门工具包后,我了解到 servlet 相关代码应该放在 app.js 文件中。但我不确定如何编写数据库查询以及如何从数据库中获取详细信息并显示到另一个页面上。任何帮助,将不胜感激。谢谢。

4

1 回答 1

0

对于检索数据,您需要实现如下逻辑:

  html/jade file:
   // USER INFO
    #userInfo
        h2 User Info
        p
            strong Name:
            |  <span id='userInfoName'></span>
            br
            strong Age:
            |  <span id='userInfoAge'></span>
            br
            strong Gender:
            |  <span id='userInfoGender'></span>
            br
            strong Location:
            |  <span id='userInfoLocation'></span>
    // /USER INFO

js中对应的函数代码: // 显示用户信息 function showUserInfo(event) {

// Prevent Link from Firing
event.preventDefault();

// Retrieve username from link rel attribute
var thisUserName = $(this).attr('rel');

// Get Index of object based on id value
var arrayPosition = userListData.map(function(arrayItem) { return arrayItem.username; }).indexOf(thisUserName);

// Get our User Object
var thisUserObject = userListData[arrayPosition];

//Populate Info Box
$('#userInfoName').text(thisUserObject.fullname);
$('#userInfoAge').text(thisUserObject.age);
$('#userInfoGender').text(thisUserObject.gender);
$('#userInfoLocation').text(thisUserObject.location);

};

以下是参考文档: http ://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/

一旦您的应用程序在本地运行,您可以通过 cloudfoundary 工具将其推送到 Bluemix:http: //clouds-with-carl.blogspot.in/2014/02/deploy-minimal-nodejs-application-to.html

希望能帮助到你。

于 2014-11-19T06:56:11.757 回答