Guest User

Untitled

a guest
Jan 14th, 2017
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. HTML
  2.  
  3. <!DOCTYPE html>
  4. <html>
  5. <head>
  6. <title></title>
  7. <script type="text/javascript" src="client.js"></script>
  8. </head>
  9. <body>
  10. <div id="container">
  11. <div id="registration">
  12. <form action = "/insert" id="signup">
  13. <label>Name <input type="text" name="name" value="foo" id="name"></label>
  14. <input type="submit" value="Sign Up" id="submit"></label>
  15. </form>
  16. </div>
  17. </div>
  18. </body>
  19. </html>
  20.  
  21. CLIENTJS
  22.  
  23. window.addEventListener('load', function() {
  24. document.getElementById('signup').addEventListener('submit', function(e) {
  25. e.preventDefault();
  26. var xhr = new XMLHttpRequest();
  27. xhr.open('POST', '/insert', true);
  28. xhr.onload = function () {
  29. // do something to response
  30. console.log(this.responseText);
  31. };
  32. xhr.send(new FormData(document.getElementById('signup')));
  33. });
  34. });
  35.  
  36.  
  37. SERVERJS
  38.  
  39. var http = require('http');
  40. var mysql = require('mysql');
  41. var express = require('express');
  42. var app = express();
  43. var bodyParser = require('body-parser');
  44. var bodyparser=bodyParser.urlencoded({extended: false});
  45.  
  46.  
  47. var connection = mysql.createConnection({
  48. host: 'localhost',
  49. user: 'root',
  50. password: '12345',
  51. database: 'employee'
  52. });
  53.  
  54. console.log('MySQL Connection details ');
  55.  
  56. app.get('/:id', function(request, response) {
  57. connection.query('SELECT * FROM employee WHERE id='+request.params.id, function(err, rows, fields) {
  58. console.log('Connection result error '+err);
  59. response.end("result"+JSON.stringify(rows))
  60. });
  61. })
  62.  
  63.  
  64. app.post('/insert',function(req, res) {
  65. var post = req.params.name;
  66. console.log(post);
  67. var query =connection.query('INSERT INTO employee(name)VALUES("'+post+'")', function(err, result) {
  68. console.log(err);
  69. });
  70. });
  71.  
  72. app.use('/client', express.static(__dirname + '/client'));
  73.  
  74. app.listen(8081);
Add Comment
Please, Sign In to add comment