-
January 1st, 2016, 08:59 AM
#1
How to post data from client to another page?
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:
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');
});
});
});
});
User.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>
Admin.html:
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>
Just wondering if anyone has any ideas on how to do this?
-
January 5th, 2016, 04:10 PM
#2
Re: How to post data from client to another page?
Honestly, if your not using standard GET and POST transactions (which you aren't) then in this case you should:
#1 Send Socket Transactions to Server.
#2 Server Accepts Socket Transaction and inserts answers into database.
#3 Server Sends back reciept of data
#4 Now when Administrator page is browsed on server, client info is found, because
Admin page just reads database.
That is one way you could make this work.
Other than that read up on GET and POST, which are more standard methods of posting data over HTTP.
http://www.w3schools.com/tags/ref_httpmethods.asp
Last edited by ahoodin; January 5th, 2016 at 04:15 PM.
ahoodin
To keep the plot moving, that's why.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|