I have a simple HTML like this:
<html>
<head><title></title></head>
<body>
<script>var testVariable = "Hello"</script>
<form method="post" action="/">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
And my Node.js looks like this:
app.post('/', function(req, res) {
Console.log(req.body.testVariable);
});
What I need to do is when the form is submitted the testVariable
needs to be passed to the Node.js function, I'm trying with hidden fields but I'm still having problems with that, i.e:
<input type="hidden" name="chat" value="<script>testVariable</script>">
But as you can imagine it pass all the script as a string, and not the value of the variable.
Does someone knows how to do that? Sorry if this is a silly question, I'm new to JavaScript and Node.js in general and I can't find answers in Google.
Thanks.
-----EDIT------
My form now looks like this:
<form method="post" action="/">
<input type="hidden" name="chat" id="hiddenInput" />
<script>
var input = document.getElementById('hiddenInput');
input.value = $('#conversation');
</script>
<input type="submit" name="submit" value="Submit">
</form>
And on my Node.js I'm printing the object like this:
console.log(JSON.stringify(req.body.chat));
and it prints "[object Object]"
(including the quotes).
I verified that the variable received is a string with:
console.log(typeof req.body.chat); // prints "string"
I have a simple HTML like this:
<html>
<head><title></title></head>
<body>
<script>var testVariable = "Hello"</script>
<form method="post" action="/">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
And my Node.js looks like this:
app.post('/', function(req, res) {
Console.log(req.body.testVariable);
});
What I need to do is when the form is submitted the testVariable
needs to be passed to the Node.js function, I'm trying with hidden fields but I'm still having problems with that, i.e:
<input type="hidden" name="chat" value="<script>testVariable</script>">
But as you can imagine it pass all the script as a string, and not the value of the variable.
Does someone knows how to do that? Sorry if this is a silly question, I'm new to JavaScript and Node.js in general and I can't find answers in Google.
Thanks.
-----EDIT------
My form now looks like this:
<form method="post" action="/">
<input type="hidden" name="chat" id="hiddenInput" />
<script>
var input = document.getElementById('hiddenInput');
input.value = $('#conversation');
</script>
<input type="submit" name="submit" value="Submit">
</form>
And on my Node.js I'm printing the object like this:
console.log(JSON.stringify(req.body.chat));
and it prints "[object Object]"
(including the quotes).
I verified that the variable received is a string with:
console.log(typeof req.body.chat); // prints "string"
Share
Improve this question
edited Dec 1, 2014 at 22:46
CIOC
asked Dec 1, 2014 at 22:05
CIOCCIOC
1,4074 gold badges21 silver badges52 bronze badges
1
-
The data is posted as variables named based on the
name
attribute of input elements with values defined by the elements value property. – Cory Danielson Commented Dec 1, 2014 at 22:08
2 Answers
Reset to default 4Use javascript to target the hidden input and set the value before submitting the form:
HTML
<input type="hidden" name="chat" id="hiddenInput" />
JS
var input = document.getElementById('hiddenInput');
input.value = testVariable.toString();
Also, on your Node server, you need to access req.body.chat
- the body
property you want corresponds to the name
of the input element
You need to use the input name
attribute. And that input must be inside the form
so you can catch it in Node:
<input type="hidden" name="testVariable" value="the value string">
Try:
<form method="post" action="/">
<input type="hidden" name="testVariable" value="the value string">
<input type="submit" name="submit" value="Submit">
</form>
And it should log "the value string"
in Node.