0

I have a page with two select menus and a table to display data. I fetched the data with 'get' from MySQL and populated the select menu, now when I click on the 'submit' button, it should display the data in the table below without refreshing the page. I am new to this.

App.js

    app.get('/home', function(req, res){
     db.connect(function(err){
      var sale = req.query.cbosale;
      var company = req.query.cbocompany;
       db.query("SELECT DISTINCT(SaleNo) FROM tsales; SELECT DISTINCT(Company) FROM tcompany; SELECT * FROM trecords WHERE SaleNo = '"+sale+"' AND Company = '"+company+"'", [1,2,3], function(err, result, fields){
        res.render('home', {title:"Home",data:result});
       })
      })
   })

Home.jade

    script.
     $('#submit').click(function(){
      var cbosale = $('#cbosale').val();
      var cbocompany = $('#cbocompany').val();
      $.get("/home", {cbosale: cbosale, cbocompany: cbocompany), function(data){
       $('#showdata').show();
      })
     })
    })

When I insert static variables in the query, the data gets displayed, passing params doesn't display it. Might be an issue when fetching the params.

RedCode
  • 13
  • 4

1 Answers1

0

There are two issues with your code i've spotted immediately.

The most important - not on topic, but it's important - your server is vulnerable to SQL injection - this is major security flaw and you should fix it. See for example: Preventing SQL injection in Node.js

Regarding your main question. You're not consuming result of request at all. The data of your callback is not used at all. Your $.get callback should look similar to this:

function(data){
    $('#dataTitle').val(data.title);
    $('#dataResult').val(data.result); // assuming result is plain string or number 
    $('#showdata').show();
}
Zbigniew Zagórski
  • 1,921
  • 1
  • 13
  • 23
  • Its still not working, one thing i noticed, when i run the page normally, the url is like this : "http://localhost:3000/home". But, when i edit the url like this, "http://localhost:3000/home?cbosale=13", the data gets displayed. Regardless, the query in the console.log shows the parameter in the query in both the cases. – RedCode Jul 23 '18 at 04:11