Sorry for my bad English as I'm not native English speaker.
The question is this, I have plan to do a three multiple select box in a single page, how to retrieve the query data before hand when the query in php is executed, individual query loop result will be add into a multidimensional array. 2nd, when the user click on any one of the option on the 1st multiple select box, it will structure the 2nd select box accordingly to the by calling out reference from array, how do i work on this? Lastly, I would like to do this without using ajax.
Here's part of my code, Javascript/jquery + php
$(document).ready(function(){
var selectValues = { "1" : "General Health",
"2": "Head and Neck",
"3": "Ear, nose and throat" ,
"4": "Stomach, bowel and bladder",
"5": "Bones and muscles",
"6": "Mental Health or confusion",
"7": "Pregnancy Problem",
"8": "Accident, wound or injury"
};
var $cateSymptom = $('#cateSymptom');
var $dropdownSymptom = $("#dropdownSymptom");
$.each(selectValues, function(key, value) {
$('#cateSymptom')
.append($("<option></option>")
.attr("value",key)
.text(value));
});
<?php
$query = "select a.*, asy.*, s.* from ailment as a join symptom_ailment as asy on a.ailment_id = asy.ailment_id join symptom as s on asy.symptom_id = s.symptom_id";
$result = mysqli_query($conn,$query) or die(mysqli_error($conn));
while($row = mysqli_fetch_assoc($result))
{
$sid = $row["symptom_id"];
$sname = $row["symptom_name"];
$stype = $row["stype_id"];
$aname = $row["ailment_name"];
$aid = $row["ailment_id"];
echo "<script>alert('$sid $sname $stype $aname $aid'); </script>";
?>
var selectValues2 = { "<?php echo $stype; ?>" :
{
"<?php echo $sname ?>" :
[
"<?php echo $aid ?>",
"<?php echo $aname; ?>"
]
}
};
<?php }
?>
$cateSymptom.change(function() {
alert('1');
$dropdownSymptom.empty().append(function() {
alert('2');
var output = '';
console.debug(selectValues2);
$.each(selectValues2[$cateSymptom.val()], function(key, value) {
alert('3');
output += '<option>' + key + '</option>';
});
return output;
});
}).change();
});
HTML:
<div id="scCategory">
<h3>Choose Symptoms Category</h3>
<form name="frmSC2" method="POST" id="frmSC2">
<select multiple name="symp[]" id="cateSymptom" style="width:230px;height:280px;">
</select>
</div>
<div id="scDepth">
<h3>List of Symptoms</h3>
<select multiple name="symptom[]" id="dropdownSymptom" style="width:230px;height:280px;">
</select>
</div>
<div id="scCondition">
<h3>Possible Condition</h3>
<select multiple name="condition[]" id="dropdownCondition" style="width:230px;height:240px;">
</select>
</div>