0

part of the code from country.js

router.get('/cnm',function(req,res){
      var db = req.db;
  var collection = db.get('isatreaty');
  console.log(req.query.cnm);
  var xcnm = req.query.cnm;
  console.log(xcnm);

  if(xcnm) { 
                      collection.find
                      (
                        {cnm:xcnm},function(e,docs)
                        {
                        res.render('cnm',{"cnm":docs});
                        }
                      ); 
            }
                    else 
            {
                      collection.find
                      (
                        {},function(e,docs)
                        {
                        res.render('cnm',{"cnm":docs});
                        }
                      );
            }
});
/* get one country name page */

part of the code in my cnm.ejs file.

<p id="result1">IN</p>
    
    <select id="country">
      <% cnm.forEach(function(data){ %>
        <option value="<%= data.cnm %>">
          <%= data.cnm %>
        </option>
        <% }) %>
    </select>
   
    <script>
      function GetSelectedValue() {
        var e = document.getElementById("country");
        
        const result = e.options[e.selectedIndex].value;
        document.getElementById("result1").innerHTML = result;

      }
    </script>
    <button type="button" onclick="GetSelectedValue()">Get Selected Value</button>
    
    <br>
    <br>
    <br>

It is working fine for the listing all the countries details. However I wish to select one country and for that I am using dropdown box in cnm.ejs file. It is a dropdown box populated from mongodb database on all the country names only. I am able to select the country of my choice and it is displayed on the screen. Now need two things to go further:

Either I generate a url "localhost:3000/cnm/?cnm=**** (where ***** means the name of the country chosen from the selected dropdown box) and I handle it in country.js or

I should be able to access the value stored in "result" variable, outside javascript script in my cnm.ejs file, so that I may search the database on this value in cnm.ejs file through ejs commands. I am using brute method like "localhost:3000/cnm?cnm="India" in the url directly and getting the result.

Do laugh at me. I started in computer in 1980 and now I am 68 years.

  • "Either I generate a url "localhost:3000/cnm/?cnm=**** (" — A [regular form](https://developer.mozilla.org/en-US/docs/Learn/Forms/Your_first_form) will do that without client-side JS. – Quentin Aug 13 '21 at 12:47

0 Answers0