양식을 만들려고합니다. 정보를 입력하고 제출을 클릭하면 제출 한 내용을 보여주는 새로운 페이지가 열립니다. 맨 아래에는 입력 한 모든 연락처 정보가있는 새 페이지로 리디렉션되는 버튼이 있습니다.개체 배열 (Node.JS)을 EJS 파일에 전달하는 방법
내 JavaScript 파일에서 내 EJS 파일 (formserver.js에서 contacts.ejs)로 변수를 전달하는 데 문제가 있습니다. 내 객체 배열이 전달되지 않았다는 오류가 계속 발생합니다 (contacts.ejs 16 행).
왜 내 개체 배열이 EJS 파일로 전달되지 않는지 설명하고 도움을 줄 수있는 사람이 있는지 궁금합니다.
formserver.js
var connect = require("connect");
var logger = require("morgan");
var http = require("http");
var ejs = require('ejs');
var bodyparse = require('body-parser');
var app = connect()
.use (logger('dev'))
.use (bodyparse())
.use (serve);
http.createServer(app).listen(3000);
var allInfo = new Array;
function serve(req, res)
{
//console.log("Entered ", req.url);
var gender = req.body.gender;
var firstName = req.body.firstName;
var lastName = req.body.lastName;
var street = req.body.street;
var city = req.body.city;
var state = req.body.state;
var zip = req.body.zip;
var phone = req.body.phone;
var email = req.body.email;
var contact = req.body.contact;
var mail = req.body.mail;
var form = {gender: gender, fname : firstName, lname : lastName, street : street,
city : city, state: state, zip: zip, phone : phone, email: email, mail: mail }
if (req.url == "/mailer")
{
})
}
contacts.ejs
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> <% allInfo %> </p>
<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Phone</th>
<th>Email</th>
<th>Contact by mail?</th>
</tr>
<% for (var person in allInfo) { %>
<tr>
<td><%= allInfo[i].gender %>. <%= allInfo[i].fname %> <%= allInfo[i].lname %></td>
</tr>
<tr>
<td> <%= street %>, <%= city %>, <%= state %> <%= zip %></td>
</tr>
<tr>
<td><%= phone %></td>
</tr>
<tr>
<td><%= email %></td>
</tr>
<tr>
<td><%= mail %></td>
</tr>
<% } %>
</table>
mailer.ejs
: 여기 는 코드<!DOCTYPE html>
<html>
<body>
<form action = "/submit" method = "post">
<fieldset style = "width:500px">
<p>
<div>
<input type = "radio" name = "gender" value = "Mr"> Mr.
<input type = "radio" name = "gender" value = "Mrs"> Mrs.
<input type = "radio" name = "gender" value = "Ms"> Ms.
<input type = "radio" name = "gender" value = "Dr"> Dr.
</div>
</p>
<p>
<div>
<label for = "first">First Name: </label>
<input type = "text" name = "firstName" required>
<label for = "last">Last Name:</label>
<input type = "text" name = "lastName" required>
</div>
</p>
<p>
<div>
<label for = "street" > Street: </label>
<input type = "text" name = "street" location = "street">
<label for = "city" name = "city"> City: </label>
<input type = "text" name = "city" location = "city">
</div>
</p>
<p>
<div>
<label id = "state" for = "state" name = "state">State: </label>
<select name = "state" id = "state">
<option value = "" disabled = "disabled" selected = "selected"> </option>
<option value = "NJ""> NJ </option>
<option value = "NY"> NY </option>
<option value = "PA"> PA </option>
<option value = "CT"> CT </option>
</select>
<label for = "zip"> Zip: </label>
<input type = "text" name = "zip" location = "zip">
</div>
</p>
<p>
<div>
<label for = "phone" >Phone:</label>
<input type = "text" name = "phone" location = "phone">
</div>
</p>
<p>
<div>
<label for = "email" >Email:</label>
<input type = "text" name = "email" location = "email">
</div>
</p>
<p>
<div>
<label for = "contact" name = "contact"> How may we contact you? </label>
<input type = "checkbox" name = "contact" value = "yPhone"> Phone </input>
<input type = "checkbox" name = "contact" value = "mail"> Mail </input>
<input type = "checkbox" name = "contact" value = "yEmail"> Email </input>
<input type = "checkbox" name = "contact" value = "any" checked> Any </input>
</div>
</p>
</fieldset>
<div>
<button type = "submit" name = "submit" value = "submit"> Send me spam forever! </button>
</div>
</form>
<p><a href = "/contacts"> View list of contacts! </a></p>
</body>
</html>
submit.ejs
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div><strong> Contact Information Submitted </strong></div>
<div><strong> Name: <%= gender %>. <%= fname %> <%= lname %> </strong></div>
<div><strong> Address: <%= street %>, <%= city %>, <%= state %> <%= zip %></strong></div>
<div><strong> Contact by Phone: <%= phone %> </strong></div>
<div><strong> Contact by Mail: <%= mail %></strong></div>
<div><strong> Contact by Email: <%= email %> </strong></div>
<p><a href = "/contacts"> View list of contacts! </a></p>
</body>
</html>
정말 감사합니다! 지금 일하고있어! –