fujitsu4
fujitsu4

Reputation: 301

get value of text field on node.js

I want to write a simple program only with node.js (I don't want to use angular or another js Framework) which contains a text field.

Upvotes: 2

Views: 4308

Answers (3)

Bidhan
Bidhan

Reputation: 10697

Sending HTML as a long string is a really bad practice. But I guess you can just use plain Javascript to achieve what you want. Just use the following

result += '<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">';
result += '<link rel="stylesheet" href="https://bootswatch.com/cerulean/bootstrap.min.css">';
result += '<title> Demo </title>';
/*    result += '<script type="text/javascript"> function take_number() {';
 result += 'phone_number = document.getElementById("inputphonenumber")';
 result += 'console.log(phone_number); ';
 result += 'console.log(ok);} </script>';
 result += '<script type="text/javascript"> console.log("test");'; */
result += '</head><body><br /><br /><br /><div class="container">';
result += '<form class="form-horizontal"><fieldset><legend> Demo </legend>';
result += '<br /><h2><center>Please enter your Phone Number</center></h2> <br /> <br />';
result += '<div class="col-lg-2 col-lg-offset-5">';
result += '<input type="text" class="form-control" id="inputphonenumber" placeholder="+33XXXXXXXXX"></div>';
result += '</fieldset></form></div><br /><br /><br />';
result += '<div class="form-group"><div class="col-lg-10 col-lg-offset-6">';
result += '<button type="reset" class="btn btn-default">Cancel</button>';
result += '<button type="submit" class="btn btn-primary" onclick="alertNumber()">Submit</button></div></div>';
result += '<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>';
result += '<script> 
function alertNumber() {var input = document.getElementById("inputphonenumber").value; alert("You have entered your number: " + input);}</script>';
result += '</body></html>';

Upvotes: 0

Dave Pile
Dave Pile

Reputation: 5764

Theres a few things wrong.

Body Parser should be used like this

app.use(bodyParser.urlencoded({extended: false}));

Your form should specify the method

method="post"

Your input element should use the name attribute as this is what gets passed to the server when the form is submitted

 <input type="text" class="form-control" id="inputphonenumber" name="inputphonenumber" 

put the closing form tag after the submit button

<button type="submit" class="btn btn-primary">Submit</button></form>

Dont write your html code as one big var :)

Upvotes: 1

Matt L
Matt L

Reputation: 21

This isn't so much a Node question as it is a Javascript question, as the part you need help with is executed on the client side. Take a look at this question: jQuery get textarea text. It seems to be exactly what you're looking for. I just want to add - you really should learn something like Express and its res.render() function using templates. Writing HTML and Javascript in Javascript is not a good way to accomplish what you want.

Upvotes: 1

Related Questions