Pass variables from JavaScript to Node.js -
i have simple html 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 node.js looks this:
app.post('/', function(req, res) {     console.log(req.body.testvariable); });   what need when form submitted testvariable needs passed node.js function, i'm trying hidden fields i'm still having problems that, i.e:
<input type="hidden" name="chat" value="<script>testvariable</script>">   but can imagine pass script string, , not value of variable.
does knows how that? sorry if silly question, i'm new javascript , node.js in general , can't find answers in google.
thanks.
-----edit------
my form looks 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 node.js i'm printing object this:
console.log(json.stringify(req.body.chat));   and prints "[object object]" (including quotes).
i verified variable received string with:
console.log(typeof req.body.chat);  // prints "string"      
use javascript target hidden input , set value before submitting form:
html
<input type="hidden" name="chat" id="hiddeninput" />   js
var input = document.getelementbyid('hiddeninput'); input.value = testvariable.tostring();   also, on node server, need access req.body.chat - body property want corresponds name of input element
Comments
Post a Comment