[eluser]felyx[/eluser]
Here is the js code:
Code:
$(document).ready(function() {
$("select[name=category]").change(function() {
//var optionArray = {
//"Value 1" : "Text 1",
//"Value 2" : "Text 2",
//"Value 3" : "Text 3"
//};
$("select[name=subcategory]").children().remove();
$("select[name=subcategory]").addOption('Please Choose!', 'Please Choose!');
var selcat = $("select[name=category]").val();
if (selcat != 'Please Choose!')
{
<?php
echo 'switch(selcat){ ';
foreach($subcategories as $catname => $key) {
$scat_arr = explode(",",$key);
sort($scat_arr);
$j = count($scat_arr);
echo 'case "' . $catname . '": ';
echo 'var optionArray = { ';
$i = 1;
$j = count($scat_arr);
foreach($scat_arr as $scat_item) {
echo '"' . $scat_item . '" : "' . $scat_item . '"';
echo ($i < $j) ? ', ' : '';
$i++;
}
echo '}; ';
echo 'break; ';
}
echo '}';
?>
// Use true if you want to select added options
$("select[name=subcategory]").addOption(optionArray, false);
}
});
});
I know I got errors in my code because im still trying to figure out a solution and I also know that my category system is very much lame but thats not the problem atm. If you can, please help me about this problem.
Basically I need to be able to set a default value for selects if the data is set in the database (like set_value('', default) for other input elements).