Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.13 KB | None | 0 0
  1. //index
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>StudentWebApp - Create and Update</title>
  6. <meta charset="utf-8" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <meta name="description" content="SPA MVC REST Web App">
  9. <meta name="keywords" content="HTML,CSS,XML,JavaScript">
  10. <meta name="author" content="John Doe">
  11. <link rel="stylesheet" type="text/css" href="Css/Reset.css" />
  12. <link rel="stylesheet" type="text/css" href="Css/View.css" />
  13. <script src="Js/muddle.js"></script>
  14. </head>
  15. <body>
  16. <header>
  17. <img src="Img/logo.png" alt="application logo" /><h1>Students management app</h1>
  18. </header>
  19. <nav>
  20. <ul id="navTabs">
  21. <li id="btnShowStudentPage" onclick="showStudentPage()">Create / Update</li>
  22. <li id="btnShowStudentsPage" onclick="showStudentsPage()">Read / Delete</li>
  23. <li id="btnShowLoginPage" onclick="showLoginPage()">Login/Logout</li>
  24. </ul>
  25. </nav>
  26. <main>
  27. <!-- User Message Page -->
  28. <section id="pgMessage" class="error">
  29. <span id="lblMessage">
  30.  
  31. </span>
  32. <span id="btnHideMessagePage" onclick="hideMessagePage()">
  33. ×
  34. </span>
  35. </section>
  36.  
  37. <!-- Create Update Page -->
  38. <section id="pgStudent">
  39. <h2>Student information</h2>
  40. <form>
  41. <ul>
  42. <li>
  43. <label for="txtStudentName">What's your name? <abbr title="required">*</abbr></label>
  44. <input type="text" id="txtStudentName" placeholder="your name" />
  45. </li>
  46. <li>
  47. <label for="txtStudentAge">How old are you? <abbr title="required">*</abbr></label>
  48. <input type="text" id="txtStudentAge" placeholder="your age" />
  49. </li>
  50. <li>
  51. <input type="button" id="btnSubmitStudent" value="Send information" onclick="sendData()"/>
  52. </li>
  53. <li>
  54. <input type="button" id="btnResetStudent" value="Reset information" />
  55. </li>
  56. </ul>
  57. <small>All fields marked with an asterisk (*) are required.</small>
  58. </form>
  59. </section>
  60.  
  61. <!-- Read Delete Page -->
  62. <section id="pgStudents">
  63. <h2>Students list</h2>
  64. <table id="studentsList">
  65. <tr>
  66. <th>Name</th>
  67. <th>Age</th>
  68. <th>Update</th>
  69. <th>Delete</th>
  70. </tr>
  71. <tr>
  72. <td>Alfreds Ferkiste</td>
  73. <td>24</td>
  74. <td class="update"></td>
  75. <td class="delete"></td>
  76. </tr>
  77. <tr>
  78. <td>Maria Anders</td>
  79. <td>32</td>
  80. <td class="update"></td>
  81. <td class="delete"></td>
  82. </tr>
  83. </table>
  84. </section>
  85. <section id="pgLogin">
  86. <h2>Login Page</h2>
  87. <form>
  88. <ul>
  89. <li>
  90. <label for="txtUsername">What's your username? <abbr title="required">*</abbr></label>
  91. <input type="text" id="txtUsername" placeholder="your username" />
  92. </li>
  93. <li>
  94. <label for="txtPassword">What's your password? <abbr title="required">*</abbr></label>
  95. <input type="text" id="txtPassword" placeholder="your password" />
  96. </li>
  97. <li>
  98. <input type="button" id="btnLogin" value="Login" />
  99. </li>
  100. <li>
  101. <input type="button" id="btnLogout" value="Logout" />
  102. </li>
  103. </ul>
  104.  
  105. </form>
  106. </section>
  107. </main>
  108. <footer>
  109. <small>Copyright "Ștefan cel Mare" University of Suceava</small>
  110. </footer>
  111. </body>
  112. </html>
  113. //--------------------------------------------------------------------------------------------------------------------------------------------
  114.  
  115.  
  116. //javascript
  117. // a shortcut for html element (dom = document object model)
  118. function dom(id) {
  119. return document.getElementById(id);
  120. }
  121.  
  122. function showStudentPage() {
  123. dom("pgStudent").style.display = "block";
  124. dom("pgStudents").style.display = "none";
  125. dom("pgLogin").style.display="none";
  126. }
  127. function showStudentsPage() {
  128. dom("pgStudent").style.display = "none";
  129. dom("pgStudents").style.display = "block";
  130. dom("pgLogin").style.display="none";
  131. }
  132. function showLoginPage()
  133. {
  134. dom("pgStudent").style.display="none";
  135. dom("pgStudents").style.display="none";
  136. dom("pgLogin").style.display="block";
  137. }
  138. function hideMessagePage() {
  139. dom("pgMessage").style.display = "none";
  140. }
  141.  
  142.  
  143. function sendData() {
  144. let errorMessages = getErrorMessages();
  145. if (errorMessages !== "") {
  146. // if there are errors, show them in msg div
  147. dom("lblMessage").innerHTML = errorMessages;
  148. } else {
  149. // if all values are ok
  150. // create an object with values
  151. let name = dom("txtStudentName").value;
  152. let age = dom("txtStudentAge").value;
  153. //let spec = dom("spec").options[dom("spec").selectedIndex].innerHTML;
  154. let student = {
  155. name: name,
  156. age: age
  157. //spec: spec
  158. };
  159. console.log(student);
  160. // the xhr object will serve for performing HTTP request
  161. let xhr = new XMLHttpRequest();
  162. // the request will be of type POST and will address "/student" route
  163. xhr.open("POST", "/student");
  164. // the request will send json, UTF8 data
  165. xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  166. // transmit the object converted to JSON
  167. xhr.send(JSON.stringify(student));
  168. // every time when a change in HTTP request status occures
  169. xhr.onreadystatechange = function () {
  170. // if HTTP request is DONE
  171. if (xhr.readyState === 4) {
  172. // if status is ok
  173. if (xhr.status === 200) {
  174. dom("lblMessage").innerHTML = "Datele au fost salvate cu succes.";
  175. // if status is not ok
  176. // display error message and the response from server
  177. } else {
  178. dom("lblMessage").innerHTML = "Eroare server la salvarea datelor." + xhr.responseText;
  179. }
  180. }
  181. };
  182. }
  183. }
  184.  
  185. function getErrorMessages() {
  186. // this variable will sum up all the error messages
  187. let errorMessages = "";
  188. // perform 'name' validation
  189. if (!isNameValid()) {
  190. errorMessages += "Numele trebuie să aibă lungimea cuprinsă între 3 și 50 caractere.";
  191. //markForWrongValue(dom("name"));
  192. }
  193. // perform 'grade' validation
  194. if (!isAgeValid()) {
  195. errorMessages += "Varsta incorecta";
  196. //markForWrongValue(document.getElementById("grade"));
  197. }
  198.  
  199. return errorMessages;
  200. }
  201. function isNameValid() {
  202. let name = dom("txtStudentName");
  203. if (name.value.length < 3 || name.value.length > 50) {
  204. return false;
  205. }
  206. return true;
  207. }
  208. function isAgeValid() {
  209. let age = dom("txtStudentAge");
  210. if (isNaN(age.value) || age.value < 10 || age.value > 100) {
  211. return false;
  212. }
  213. return true;
  214. }
  215. //-----------------------------------------------------------------------------------------------------------------------------------------
  216.  
  217.  
  218.  
  219. //server
  220. let express = require('express');
  221. let path = require('path');
  222. let bodyParser = require('body-parser');
  223. let mysql = require('mysql');
  224.  
  225. let app = express();
  226.  
  227. // Define the static folder
  228. app.use(express.static(path.join(__dirname, 'public')));
  229.  
  230. // Neded for parsing json data
  231. app.use(bodyParser.json());
  232. // define rutes
  233. app.post('/student', function (req, res) {
  234. // get data from client request
  235. let name = req.body.name;
  236. let age = req.body.age;
  237. //let spec = req.body.spec;
  238. // prepare connection to database
  239. let connectionParameters = {
  240. host: "localhost",
  241. port: 55555,
  242. user: "root",
  243. password: "sa",
  244. database: "firstdb"
  245. };
  246. // create a connection to db
  247. let con = mysql.createConnection(connectionParameters);
  248. // open the connection
  249. con.connect(function (err) {
  250. // if connection error occurs, the client must be informed
  251. if (err) {
  252. res.status(500).send("Eroare la conectarea la baza de date." + err.message);
  253. } else {
  254. // prepare the insert query
  255. let sql = "INSERT INTO students (name, grade) VALUES ('" + name + "','" + age + "');";
  256. // run query
  257. con.query(sql, function (err, result) {
  258. // if query error occurs, the client must be informed
  259. if (err) {
  260. res.status(500).send("Eroare la inserarea datelor." + err.message);
  261. // if we are here => Hooray, no errors!
  262. } else {
  263. res.status(200).send("");
  264. }
  265. });
  266. }
  267. con.query("SELECT * from students", function (err, result, fields) {
  268. if (err) throw err;
  269. console.log(result);
  270. });
  271. });
  272. });
  273.  
  274. // Inform that server is alive
  275. console.log("Server running on 8080.");
  276. // Listen to a port
  277. app.listen(8080);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement