1
node.jsのアンケートはデータベースから生成されました。質問は任意のタイプの「複数選択肢、単一選択肢、イエス・ノー・テキスト」にすることができます。質問に答えました。 私はフォームを検証する方法を知っていますが、フォームに名前、姓、電子メールのダイアログがあるので、最初の部分がjQuery/javascriptのボタンid="show-dialog"
と名前、姓、ボタン。データベースから生成されたアンケートの検証方法
マイビュー(start.ejs)は次のようになります。
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</head>
<body>
<h1><%= title %></h1>
<form name="questionsForm" id="questionsForm" method="post" action="/save">
<% pitanja.forEach(function(pitanje) { %>
<input type="hidden" name="pitanje_id" value="<%= pitanje.id %>" >
<input type="hidden" name="upitnik_id" id="upitnik" value="<%= pitanje.upitnik_id %>" >
<% switch (pitanje.tip_pitanja_id) {
case 1: %>
<div class="mdl-card mdl-cell mdl-cell--12-col mdl-card__supporting-text">
<h4> <%= pitanje.tekst %> </h4>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="odgovor_tekst" name="<%= pitanje.id %>">
<label class="mdl-textfield__label" for="odgovor_tekst">Tekst...</label>
</div>
</div>
<% break; %>
<% case 2: %>
<div class="mdl-card mdl-cell mdl-cell--12-col mdl-card__supporting-text">
<h4> <%= pitanje.tekst %> </h4>
<ul class="demo-list-control mdl-list">
<li class="mdl-list__item">
<span class="mdl-list__item-primary-content">
<label>Da</label>
</span>
<span class="mdl-list__item-secondary-action">
<label class="demo-list-radio mdl-radio mdl-js-radio mdl-js-ripple-effect" for="list-option-yes">
<input id="list-option-yes" class="mdl-radio__button" type="radio" name="<%= pitanje.id %>" value="true">
</label>
</span>
</li>
<li class="mdl-list__item">
<span class="mdl-list__item-primary-content">
<label>Ne</label>
</span>
<span class="mdl-list__item-secondary-action">
<label class="demo-list-radio mdl-radio mdl-js-radio mdl-js-ripple-effect" for="list-option-no">
<input id="list-option-no" class="mdl-radio__button" type="radio" name="<%= pitanje.id %>" value="false">
</label>
</span>
</li>
</ul>
</div>
<% break; %>
<% case 3: %>
<div class="mdl-card mdl-cell mdl-cell--12-col mdl-card__supporting-text">
<h4> <%= pitanje.tekst %> </h4>
<ul class="demo-list-control mdl-list">
<% odgovori.forEach(function(odgovor) { %>
<% if(pitanje.id == odgovor.pitanje_id){ %>
<li class="mdl-list__item">
<label class="mdl-checkbox mdl-js-checkbox mdl-js-ripple-effect" for="checkbox-<%= odgovor.id %>">
<input type="checkbox" id="checkbox-<%= odgovor.id %>" name="<%= pitanje.id %>" value="<%= odgovor.tekst %>" class="mdl-checkbox__input">
<span class="mdl-checkbox__label"><%= odgovor.tekst %></span>
</label>
</li>
<% }%>
<% }); %>
</ul>
</div>
<% break; %>
<% case 4: %>
<div class="mdl-card mdl-cell mdl-cell--12-col mdl-card__supporting-text">
<h4> <%= pitanje.tekst %> </h4>
<ul class="demo-list-control mdl-list" >
<% odgovori.forEach(function(odgovor) { %>
<% if(pitanje.id == odgovor.pitanje_id){ %>
<li class="mdl-list__item">
<span class="mdl-list__item-primary-content">
<label><%= odgovor.tekst %></label>
</span>
<span class="mdl-list__item-secondary-action">
<label class="demo-list-radio mdl-radio mdl-js-radio mdl-js-ripple-effect" for="list-option-<%=odgovor.id%>">
<input id="list-option-<%=odgovor.id%>" class="mdl-radio__button" type="radio" name="<%= pitanje.id %>" value="<%= odgovor.tekst %>">
</label>
</span>
</li>
<% }%>
<% }); %>
</ul>
</div>
<% break; %>
<% } %>
<% }); %>
</br>
<button id="show-dialog" type="button" class="mdl-button mdl-js-button mdl-button--raised mdl-button--accent">Sačuvaj</button>
<dialog class="mdl-dialog">
<h4 class="mdl-dialog__title">Unesite vaše podatke</h4>
<div class="mdl-dialog__content">
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="ime" name="ime">
<label class="mdl-textfield__label" for="ime">Ime</label>
<span class="mdl-textfield__error">Greška!</span>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="prezime" name="prezime">
<label class="mdl-textfield__label" for="prezime">Prezime</label>
<span class="mdl-textfield__error">Greška!</span>
</div>
<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
<input class="mdl-textfield__input" type="text" id="email" name="email">
<label class="mdl-textfield__label" for="email">Email</label>
<span class="mdl-textfield__error">Greška!</span>
</div>
</div>
<div class="mdl-dialog__actions">
<input type="submit" value="Spremi" class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored pull" >
<button type="button" class="mdl-button close">Prekini</button>
</div>
</dialog>
</form>
<script>
var dialog = document.querySelector('dialog');
var showDialogButton = document.querySelector('#show-dialog');
if (! dialog.showModal) {
dialogPolyfill.registerDialog(dialog);
}
showDialogButton.addEventListener('click', function() {
dialog.showModal();
});
dialog.querySelector('.close').addEventListener('click', function() {
dialog.close();
});
</script>
</body>
</html>
Node.jsのルートstart.js:JavaScriptで
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/:id', function(req, res, next) {
var pg = require('pg');
var connect = "postgres://postgres:[email protected]:5432/Questionnaire"
var client = new pg.Client(connect);
// connect to our database
var odgovori;
client.connect();
client.query('SELECT * FROM predefinisani_odgovori WHERE(Select id FROM pitanja Where upitnik_id=$1 AND id=pitanje_id) = pitanje_id order by random() limit 1000;',[req.params.id], function (err, result) {
if (err) throw err;
if(result!=null)
odgovori = result.rows;
});
client.query('SELECT * FROM pitanja WHERE upitnik_id=$1',[req.params.id], function (err, result) {
if (err) throw err;
var all = result.rows;
res.render('start',{pitanja: all,title:'Questionnaire',odgovori:odgovori})
// execute a query on our database
client.end(function (err) {
if (err) throw err;
});
});
});
module.exports = router;