How about with just straight javascript? I found a script that works, but I’m not familiar enough with javascript to do one final step on it.
Here is the script part that tells it what options to choose. I believe I can put bits of php in here to generate my options, would that be correct to assume that? Or will it mess the javascript up to put in php for generating?
<script language="javascript" type="text/javascript">
function dropdownlist(listindex)
{
document.formname.subcategory.options.length = 0;
switch (listindex)
{
case "Alabama International Dragway 5-21-10 - 5-23-1" :
document.formname.subcategory.options[0]=new Option("Select Sub-Category","");
document.formname.subcategory.options[1]=new Option("Air-Conditioners/Coolers","Air-Conditioners/Coolers");
document.formname.subcategory.options[2]=new Option("Audio/Video","Audio/Video");
document.formname.subcategory.options[3]=new Option("Beddings","Beddings");
document.formname.subcategory.options[4]=new Option("Camera","Camera");
document.formname.subcategory.options[5]=new Option("Cell Phones","Cell Phones");
break;
case "Atlanta Dragway 4-16-10 - 4-18-10" :
document.formname.subcategory.options[0]=new Option("Select Sub-Category","");
document.formname.subcategory.options[1]=new Option("Colleges","Colleges");
document.formname.subcategory.options[2]=new Option("Institutes","Institutes");
document.formname.subcategory.options[3]=new Option("Schools","Schools");
document.formname.subcategory.options[4]=new Option("Tuitions","Tuitions");
document.formname.subcategory.options[5]=new Option("Universities","Universities");
break;
}
return true;
}
</script>
Then I have this now for my dropdowns:
$dir = 'gallery/';
$categories = scandir($dir);
$categories = array_slice($categories, 2);
if (!isset($_GET['subpage'])) { // Image Upload Form Below
$content .= "<form method=\\"post\\" id=\\"formname\\" name=\\"formname\\" action=\\"index.php?page=gallery&add=new&subpage=upload\\" enctype=\\"multipart/form-data\\">
<p><select name=\\"category\\" onchange=\\"javascript: dropdownlist(this.options[this.selectedIndex].value);\\">\
<option>Choose a Category:</option>\
";
foreach ($categories as $value) {
if ($_GET['class'] == $value) {
$content .= "<option selected value=\\"$value\\">$value</option>\
";
} else {
$content .= "<option value=\\"$value\\">$value</option>\
";
}
}
$content .= "</select>\
</p>
<p>
<script type=\\"text/javascript\\" language=\\"JavaScript\\">
document.write('<select name=\\"subcategory\\"><option value=\\"\\">Select Sub-Category</option></select>')
</script>
<noscript>
<select name=\\"subcategory\\" id=\\"subcategory\\" >
<option value=\\"\\">Select Sub-Category</option>
</select>
</noscript>
</p>
File:<br />
<input type=\\"file\\" name=\\"imagefile\\" class=\\"form\\">
<br /><br />
<input name=\\"submit\\" type=\\"submit\\" value=\\"Sumbit\\" class=\\"form\\"> <input type=\\"reset\\" value=\\"Clear\\" class=\\"form\\">
</form>";
So I have two dropdowns, second one is generating based on the choice. This is working correctly. Will I be able to, with the javascript, keep the options chosen, selected after submitting the form? It’s an image upload script, after uploading one image, the same page reloads and I need the dropdown menus to be the same choices as I just chose. I could do this with one and before added the javascript, pretty easy with this part:
if ($_GET['class'] == $value) {
$content .= "<option selected value=\\"$value\\">$value</option>\
";
} else {
$content .= "<option value=\\"$value\\">$value</option>\
";
}
Would I be able to do something as simple as that in my top javascript bit when I do the php part?