Just thought I'd share something I figured out today. In administration "edit item" interfaces, you often have select boxes, and you want to select the item that corresponds with the entry for that field in the database. I used to do this purely with PHP (and thus having to compare each and every item in the options list). I figured out that using a javascript function, and then writing out calls to that function (dynamically using PHP) is much much quicker.
Here's the javascript function I created:
// javascript not php
function showSelectBoxSelectedItem(sbox, valueToSet){
for (loop = sbox.options.length-1; loop > 0; loop--) {
if(sbox.options[loop].value==valueToSet){
sbox.selectedIndex=loop;
break;
}
}
}
Then, at the bottom of the page (to make sure the form field you are targeting has already loaded), add PHP similar to this to write out your javascript function calls.
echo "<script language='javascript'>\n\t";
echo "showSelectBoxSelectedItem(document.forms[0].InterviewYear,'".substr($myrow['InterviewDate'],0,4)."')\n\t";
echo "showSelectBoxSelectedItem(document.forms[0].InterviewMonth,'".substr($myrow['InterviewDate'],5,2)."')\n\t";
echo "showSelectBoxSelectedItem(document.forms[0].InterviewDay,'".substr($myrow['InterviewDate'],8,2)."')\n";
echo "</script>";
?>
Hope this helps someone. Sure seems to save a lot of time (in a non-templated environment).