Can you check below code? I added comments about the errors you listed.
var map = L.map('map').setView([39.707045, -104.940817], 12);
var sql_text = '1=1';
var topPane = map.createPane('leaflet-top-pane', map.getPanes().mapPane);
var bottomPane = map.createPane('leaflet-bottom-pane', map.getPanes().tilePane);
//load maps
//load google basemap
load_schools();
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
function load_schools() {
//defeine schools as a global variable, without var keyword so it can be checked at set_sql function
schools = L.tileLayer.wms('http://open-geode.dpsk12.org/geoserver/ows?', {
layers: 'dps-sm:Schools_Current',
transparent: true,
format: 'image/png',
cql_filter: sql_text,
zIndex: 600,
opacity: 1
}).addTo(map);
};
function set_sql() {
//Cause of error "the basemap disappears"
//this part removes all of the layers(including base layer), so comment it out
//map.eachLayer(function (layer) {
// map.removeLayer(layer);
//});
sql_value = document.getElementById('sql_input').value;
sql_text = 'schnum = ' + sql_value;
console.log(sql_text);
//Remove old schools to prevent "a new point is added but the old one is not removed"
//check if map has schools layer
map.hasLayer(schools)
{
map.removeLayer(schools); //if so, remove previously added schools layer before loading schools with new cql filter
}
load_schools(sql_text);
}