I have a list that is populated from a database array like so:
$i = 0;
foreach ($category_label as $value) {
echo '<input id="'.$category_label[$i].'" type="checkbox" name="q1" value="'.$category_label[$i].'">
<label for="'.$category_label[$i].'">'.$category_label[$i].'</label><br />';
$i++;
As you can see, this builds a list of items with a checkbox by them from the database array. The next step is that if a user selects one of the checkboxes, I need to display a new array of associated items. Here is the javascript and associated <div>
that I have started to do this:
JS:
$(function() {
$('input[name=q1]').on('change', function () {
var valu = this.value;
if (this.checked) {
$('#q2').append("<li><a href='#'>" + valu + "</a></li>");
$('#question2').show();
} else {
var lis = $("#q2").find('li');
lis.each(function () {
if ($(this).text() === valu) {
$(this).remove();
}
});
if (lis.length === 0) {
$('#question2').hide();
}
}
});
});
Associated PHP:
<div id="question2" class="question">
<p>Results:</p>
<ul id="q2"></ul>
</div>
Okay, so right now, the var valu = this.value;
in the js
script is showing the selected item in the question2
div. Rather, I need to get the category id value from the checked item and then pull all the child items that have the associated parent category to fill out the list. How can I modify my php and js to do so?
UPDATE:
This question is getting some feedback that it is too broad. Let me specify the issue a bit further to find a good solution. I have modified the first PHP script to pull the category id from the selected item into the input item as the value. Here is that update:
echo '<input id="'.$category_label[$i].'" type="checkbox" name="q1" value="'.$category_id[$i].'"><label for="'.$category_label[$i].'">'.$category_label[$i].'</label><br />';
Now, I just need to find a way to pass this value through the javascript and get the array of items that are associated with that category value to display in the output list.