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.