1

I am attempting to perform specific queries to a mySQL database based on what a user selects from my website's select options.

HTML:

<select id = "year">
<option value = "yr" selected>Choose a Year</option>
<option id = "2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
</select>

Node.JS:

    function handleDatabase(request,response){
     pool.getConnection(function(err, connection){
    //Check if there's an error, if so, stop connection and print error
      if(err){
        connection.release();
        response.json({"code": 50, "status": "Error in connection to database"});
        return;
    }
    connection.query("select * from 'fifteen' where TYPE = 'Mischief'", function(err, rows){
      connection.release();
      if(!err){
        response.json(rows);
      }
    });
      response.json({"code": 50, "status" : "Error in connection to database"});
      return;
    });
  });
}

app.get('/',function(request,response){
    handleDatabase(response,request);
    }

AJAX:

$(document).ready(function(){
    $('#year').click(function() {
            $.ajax({
                type: 'GET',
                url: 'http://localhost:8888/',
                dataType: 'json',
                success: function () {
                    console.log("Success");
                },
                error: function (data) {
                    console.log("Error.", data);
                }

            });
    });
});

I would like the user to be able to select which ever year they want, and then perform an AJAX request onto the server which would then perform a query onto the database. For example, the user selects 2014, make an AJAX request to the server, the server then performs a query onto the table, fourteen, and returns all data from it.

1 Answer 1

1

One solution would be to just use express middleware for dynamic url params:

var express = require('express');
var router = express.Router();

router.get('/yearQuery/:year', function(req, res) {
  var year = req.params.year;

  // Your code

  res.json(//year data object);
});

Ajax request becomes:

$.ajax({
  type: 'GET',
  url: 'http://localhost:8888/yearQuery/' + year,
  dataType: 'json',
  success: function () {
    console.log("Success");
  },
  error: function (data) {
    console.log("Error.", data);
  }              

I leave it to you to fill out the rest (such as the document id selector for year value).

Sign up to request clarification or add additional context in comments.

4 Comments

Could you elaborate a little bit more for me? I'm extremely new to all of this. For the router you added, does that require the user to manually type in their desired year at the end of the url? And when you do "/yearQuery/ + year", is that year variable pre-defined or to be filled in by myself?
The user is just picking the year from one of your buttons or a selector. Your code is creating the url.
The year variable is populated by the url.
You need to use standard jquery to select the selected value from your selecter, create a VAR from that value, name that var year, and that is the year variable in your ajax url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.