I am currently working on a project using nodejs, express, and mongodb within the cloud9 IDE.
My goal is to create a page with a form that allows users to input data, search for corresponding information in the database, and display that data on the same page after reloading. If the data is not found, I'd like to show a specific response.
This is what I have accomplished so far:
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");
});
However, I'm experiencing an issue where the response indicating no data found only appears briefly before the page reloads. I haven't been able to find a solution despite researching it. Any help would be appreciated.