0

I am taking my first jab at Express Node JS and thoroughly enjoying it. I'm looking to understand how to get first name and last name information input by a user to the server and displaying it in the console log.

My code in HTML is as follows:

<form action = "/getname/" method = "GET"> 
    <p>First name: <input type="text" id="fname" name="firstname"></p> 
    <p>Last name: <input type="text" id="lname" name="lastname"></p>
    <p><input type="button" value="Submit" id="namebutton" /></p>
    <p><span id="JSONname"></span></p>
</form>

My code in Express app.js is:

app.get('/getname/', function (req, res) {
    response = {
        firstname: req.query.firstname,
        lastname: req.query.lastname
    };
    console.log(response);
    res.end(JSON.stringify(response));
})

I think the connection between the localhost:xxxx/getname and the server is working, however the html to getname isn't working.

Might anyone see where the code is going wrong?

1
  • Moreover, Submitting form data using the GET method is not an advisable one. If we GET the set of data will be visible in URL without encryption and it is in-secure to handle data. Instead, you can use the POST method. Commented Jan 7, 2020 at 10:20

1 Answer 1

1

The submit button must be type="submit" and not type="button".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.