I'm new to using node and sockets but I've managed to create a working code where a user can input their name, email and comment and it gets sent to the server, command line logs what was inputted and also sends a popup notification to the admin page saying they have a new message. This is exactly what I want but I'm having trouble having the data appear on the admin page rather than just showing it on the command line. Here is my code:
Server.js:
User.html:Code:var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); app.get('/', function (req, res) { res.sendFile(__dirname + '/user.html'); }); app.get('/admin', function (req, res) { res.sendFile(__dirname + '/admin.html'); }); io.on('connection', function(socket){ console.log("Connected..."); socket.on('Name', function(msg){ console.log('Name: ' + msg); socket.on('Email', function(msg){ console.log('Email: ' + msg); socket.on('Comment', function(msg){ console.log('Comment: ' + msg); socket.broadcast.emit('Message', 'Message from the User'); }); }); }); });
Admin.html:Code:<html> <head> <title>User Page</title> <script src="https://cdn.socket.io/socket.io-1.3.7.js"></script> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script> $(document).ready(function(){ var socket = io.connect('http://localhost:8000'); $('#form').submit(function(){ socket.emit('Name', $('#Name').val()); socket.emit('Email', $('#Email').val()); socket.emit('Comment', $('#Comment').val()); return false; }); }); </script> </head> <body bgcolor="e5ffff"> <h1>User Page</h1></font> <form id = "form" action=""> Name: <input type = "text" id="Name"><br> <br> Email: <input type = "text" id="Email"><br> <br> Comment: <input type = "text" id="Comment"><br> <br> <input type = "Submit" value = "Send"> </form> </body> </html>
Just wondering if anyone has any ideas on how to do this?Code:<html> <head> <title>Admin Page</title> <script src="https://cdn.socket.io/socket.io-1.3.7.js"></script> <script src="//code.jquery.com/jquery-1.11.3.min.js"></script> <script> $(document).ready(function(){ var socket = io.connect('http://localhost:8000'); socket.on('Message', function(message) { alert('The server has a message for you: ' + message); }); }); </script> </head> <body> <h1>Admin Page</h1> <p>Please review the feedback and edit at your will...</p> <!--Where users submission form will be viewed--> </body> </html>




Reply With Quote
