user5473154 user5473154 - 5 months ago 13
HTML Question

node/express - How to render user input to a page?

I am trying to render some text using node/express.
I have a html file contains a form.

search.ejs

$(document).ready(function(){
var userInput;
$("#submit-button-id").click(function(){
userInput = $1_11_1("#userInput").val();
$.get("http://localhost:3000/searching", {
userInput: userInput
}, function(){});
});
});


app.js

app.get('/searching', function(req, res){
var userInput = req.query.userInput;

/** I am able to get the userInput (the code above works) */

res.send(userInput);

/** I want to render the userInput (for instance, on localhost:3000/results) */
});


Any help/links would be greatly appreciated, thanks!

Answer

In your app.js, you are sending the userInput information back to search.ejs. this this location:

$(document).ready(function(){
    var userInput;
    $("#submit-button-id").click(function(){
        userInput = $1_11_1("#userInput").val();
        $.get("http://localhost:3000/searching", {
            userInput: userInput
        }, function(){***User data can be used here***});
    });
});

So the following code would work to render your raw data into a div on your html page called render-div:

$(document).ready(function(){
    var userInput;
    $("#submit-button-id").click(function(){
        userInput = $1_11_1("#userInput").val();
        $.get("http://localhost:3000/searching", {
            userInput: userInput
        }, function(userInputData){
            $('.render-div).append(userInputData);

        });
    });
});`