I hope someone can help me with this:
I'm building a website with a product comparisontable (with checkbox filters) and I'm using a Javascript I found on the internet. I have some knowledge of JavaScript, but Ajax en jQuery are new for me :-).
The code that's working:
$(document).ready(function () {
filter_data();
function filter_data() {
$('.filter_data').html('<div id="loading" style="" ></div>');
var action = 'fetch_data';
var walk = get_filter('walk');
var bike = get_filter('bike');
var swim = get_filter('swim');
var multi = get_filter('multi');
$.ajax({
url: "functions/fetch_data.php",
method: "POST",
data: {
action: action,
walk: walk,
bike: bike,
swim: swim,
multi: multi
},
success: function (data) {
$('.filter_data').html(data);
}
});
}
function get_filter(class_name) {
var filter = [];
$('.' + class_name + ':checked').each(function () {
filter.push($(this).val());
});
return filter;
}
$('.common_selector').click(function () {
filter_data();
});
});
This code is working, but ... the variables inside this function, need to be generated from an external text file. Because the variables need to be edited easily and very frequent. And there are a lot more variables than you see in this code.
And this is what my (shortened) textfile looks like:
1,brand,Brand
2,walk,Walk
2,bike,Bike
2,swim,Swim
2,multi,Multi
The numbers indicate whether the field should be used in the filter or not. Lines with number 2 are the variables, and need to be 'pasted' in the code as variables and datafields.
Is it possible to get the fields/variables from the textfile within Javascript Ajax / jQuery? So with an extra function?
I hope someone can help me with this, I've been working on it for many hours but I can't get it to work properly.
Thanks!!