For my class we're working on this weird little article updating web app using ASP. Each article that we can add to the database must have a main category and a sub category. Thats all fine and dandy but my problem is when a user wants to edit the article. Now the user should be able to edit any portion of the article (except the article date) and the same form that I use for adding articles is being used to edit an article, except when the edit link is pressed the form will be populated with the information of that specific article.
My problem is this: I can get the correct main category to load on the edit page for that article but i cant figure out a way to get the sub-category to load properly. Since the sub cat select box content always depended upon the user selecting a main cat (I have the main cat select box have an onChange="goSub(this)" parameter to fill the sub cat box) is there a way to use the same script so that on the edit page the sub cat box can be automatically changed to the correct category? Or should I write a new function?
Here's my javascript for filling the sub cat when the main cat box is changed:
Code:
<SCRIPT LANGUAGE="JavaScript">
function goSub(cat_no){
for (var i = document.addform.article_subcat.options.length; i >= 0; i--){
document.addform.article_subcat.options[i] = null;
}
if (cat_no.options[cat_no.selectedIndex].value==0){
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('Select a Main Category','0');
}
if (cat_no.options[cat_no.selectedIndex].value==1){
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('HTML','1');
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('XHTML','2');
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('XML','3');
}
if (cat_no.options[cat_no.selectedIndex].value==2){
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('Classic ASP','1');
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('ASP.NET','2');
}
if (cat_no.options[cat_no.selectedIndex].value==3){
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('MySQL','1');
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('Access','2');
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('SQL_Server','3');
}
if (cat_no.options[cat_no.selectedIndex].value==4){
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('Perl','1');
document.addform.article_subcat.options[document.addform.article_subcat.options.length] = new Option('Python','2');
}
}
</SCRIPT>