Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

110
Views
Taking input from form, retrieving data from database and showing output

Im working with nodejs ,express and mongodb.And im working on cloud9 IDE

I want to create a page with a form which takes input from the user, search corresponding data from database and show that data on same page (after reloading) ,and if data isnt present then showing an specific response.

this is what i have done-

Route-

app.get("/",function(req,res){
var asked = req.query.asked;
  Name.findOne({name:asked},function(err,foundAsked){
      if(err){console.log("ERROR!!!");}
      else{res.render("Landing.ejs",{foundAsked:foundAsked}); }
  });
 }); 

EJS file-

//Form takes name as input from the user

<form >
    <input type="text" name="asked" action="/" method="GET">
    <button class="button">Submit</button>
</form>



//If data is found last name is returned
<% if(foundAsked){ %>
    <h2 >  <%= foundAsked.Lastname %> </h2>
<% } %>


 //blank space stays there which is replaced by a response,if after 
   //submission nothing is found
<% if(!foundAsked){ %>
    <h5></h5> 
<% } %>

JS-

 $(".button").click(function(){

          $("h5").text("No data present");
 });

But the problem is that,if there is no data found ,the response is shown ,but it stays for only the time before the page reloads. I tried searching for an answer but i havnt found any.Please help

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The text you want to be shown, will appear only after the button is clicked, and once reloaded, you will have to click again (and the click will show the text, and trigger a reload, so the text will disappear again).

You need to have another button (That will not submit) to trigger this message. Or rather why not show initially (put this inline, inside the <h5> tags) ?

You need to differentiate between 'no-data' or 'no-query. you can do it in the back-end, something similar to this:

app.get("/",function(req,res){
var asked = req.query.asked;
  Name.findOne({name:asked},function(err,foundAsked){
      if(err){console.log("ERROR!!!");}
      else{res.render("Landing.ejs",{foundAsked:foundAsked, asked: asked}); }
  });
 }); 

And then in Landing.ejs:

<% if(!foundAsked && asked){ %>
        <h5>No data present</h5> 
  <% } %>
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!