Creating dynamic pages using user input in Node and Express

Currently, I am facing a challenge in rendering text using node/express. Within my project setup, there is an HTML file containing a form.

The file named search.ejs looks like this:

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

In the app.js file, here is the relevant code snippet:

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

    /** Successfully retrieving the userInput (as shown above) */

    res.send(userInput);

    /** Now, wanting to display the userInput (e.g., on localhost:3000/results) */
});

I would appreciate any assistance or resources related to this issue. Thank you!

Answer №1

Within your app.js file, the userInput information is being sent back to the search.ejs location. Here is where it happens:

$(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***});
    });
});

To display your raw data in a div on your HTML page named "render-div," you can use the following code:

$(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);

        });
    });
});`

Answer №2

If you refer to the official documentation, it states that the response is passed as a parameter in your callback function. Therefore, you can implement it like this:

$.get("http://yourdomain.com/search", {input: userInput},
    function(response){
      alert(response);
 });

Answer №3

If you are utilizing EJS, you can achieve this as shown below:

To start, in your results.ejs document

<div id="showResult"><h2><%= result %></h2></div>

Subsequently, in your app.js file

app.get('/search', function(req, res){
var userInput = req.query.inputValue;
res.render('results.ejs',{result:userInput});
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Update the JavaScript to modify the styling based on the specific value of the

Is there a way to apply specific style properties to images only if they already have another property? I've managed to achieve this with the following code snippet: if ($('#content p img').css('float') == 'right') $ ...

Implementing cross-browser COMET in node.js

As a beginner in node.js, my goal is to create an online chat platform that can handle 1000+ participants simultaneously. I am in need of a cross-browser demo that is compatible with IE7 and Android. My main concern is the client-side cross-browser functi ...

When a node using express encounters :id, it is directed to a specific location

Currently, I am in the process of creating a small API collection to familiarize myself with nodejs using express. In my project, I have included the line app.use("/v1/phrases", phraseRouter); Within the router file, the code looks like this: co ...

I continue to encounter the following error message: Error: Route.post() expects callback functions but received an [object Undefined]

Currently, I am in the process of setting up a react express application that utilizes mongo db as its database. During the initial stages, I keep encountering this particular error message: Error: Route.post() requires callback functions but got a [obje ...

Properly spaced website images

I'm trying to display my images side by side with a slight margin in between them. However, when I apply a `margin-right: 10px;` to each div, the last image doesn't line up properly with my title bar. Is there a way to create spacing between the ...

Improving React Components with Material-UI Integration

Is it possible to export a class extended from React.Component while using React and Material-UI? I need to access React variables like state within the class. If exporting the extended class is not an option, how can I still utilize state? Here is a samp ...

What is the best way to extract the src attribute from an image tag nested within innerHtml?

In the developer tools, navigate to console and enter: var x= document.getElementsByClassName('ad-area')[0].innerHTML; x returns: '<a href="/members/spotlight/311"><img class="block-banner" src="https://tes ...

What could be the reason for my ng-init not functioning properly?

<body ng-init="user=${userID};month=${month};curPageNum=${currentPage}"> Using JSP, I set initial values in the body tag. However, in the controller, I have the following code: console.debug($scope.user + " "+$scope.month} The issue is that only ...

An element in CSS that has a position of fixed and a width of 100% surpasses the dimensions of its

My element has the CSS properties position:fixed and width:100%, causing it to be larger than its parent elements. Despite the complexity of my code, I have attempted to simplify it for better understanding. Below, you can see that the green box exceeds ...

Tips for efficiently handling node-mysql database connections

Hello, I am currently in the process of developing a job recruitment application using NodeJS express framework and utilizing express-handlebars as the Rendering Engine. However, I have encountered an issue where newly posted job listings are not immediate ...

Converting a JavaScript function to CoffeeScript, accepting jQuery's .map function and Selectors as parameters

I'm in the process of converting some JavaScript code into CoffeeScript and encountering an issue with a particular function. Here is the original functioning JS: $(".comformt_QA").change(function(){ var array = $(".comformt_QA").map(function() { ...

Duplicate nodes_modules packages detected

Currently, I am utilizing npm to install numerous packages for my application. As a result, I have a node_modules directory that contains all the packages. However, some of these packages contain their own node_modules directories which in turn may contai ...

Vue: Simple ways to retrieve state data in MutationAction

I'm having trouble accessing the state inside @MutationAction Here is the setup I am using: Nuxt.js v2.13.3 "vuex-module-decorators": "^0.17.0" import { Module, VuexModule, MutationAction } from 'vuex-module-decorators' ...

Creating pathways in AJAX with Rails

Issue at hand: undefined variable article_id. The objective: Setting up the correct route for AJAX and Rails. What I require: The format articles/1/comments/2. Main goal: To specifically load comment through AJAX, not article. In the current AJAX scrip ...

Is there a way to automatically load an entire Facebook profile in one go using a program?

Does anyone know a way to automatically download all photos from a Facebook profile, not just thumbnails? I'm thinking of using wget to fetch the page source code and then extract links to images from child a elements of all div with class="rq0e ...

"Using jQuery's animate() function to dynamically change text content

I'm currently venturing into the world of jQuery animate() and decided to create a presentation-style project for practice. However, I've hit a roadblock when attempting to alter the text within my div element in the midst of an animation utilizi ...

Every time I try to upload image files to cloudinary, I encounter this frustrating error message

https://i.stack.imgur.com/kRYVZ.png The issue at hand revolves around node and the challenge of using the https module with new certificates, proving to be unsuccessful... The proposed solution is ambiguous, leaving me unsure how to proceed and resolve thi ...

Using Vue.js to submit a form in Laravel and redirecting with a flash message

I am facing an issue where I have two components named Index and Create, loaded from separate blade files. The challenge is passing a flash message as a prop between these components due to their file separation. How can I redirect after submitting a form ...

Unable to copy and paste text in a TFX-CLI console using NPM

I'm having trouble pasting text into CLI prompts when using tfx-cli. After installing Node v8.9.1 LTS, the initial installation of tfx-cli hung and I had to cancel it with Ctrl+C. The second attempt was successful, but I noticed a message about &apos ...

While Ajax POST is functional on desktop, it does not seem to work on Phonegap applications or Android

I am facing an issue with a global function that does not seem to work properly in the PhoneGap Desktop app or Chrome Mobile on Android. Surprisingly, it works perfectly fine only in the Chrome PC version. The function is called using an onClick event, a ...