Javascript/PHP: Script to set selected <SELECT> option not working

I have a select statement running which pulls data into $row.

$result = mysql_query("Select * FROM Recipes WHERE rID = $sent_rID");   
$fields_num = mysql_num_fields($result);
$error = mysql_error($link);
$row = mysql_fetch_array($result);

Next I have my SELECT block

<select name='department' id='department'>
<option value='Deli'>Deli</option>
<option value='Meat'>Meat</option>
<option value='Seafood'>Seafood</option>
</select>

Finally I have the following SCRIPT block

<SCRIPT LANGUAGE='javascript'>
DepartmentSelect($row[rDepartment]);
</SCRIPT>

And here is DepartmentSelect()

<script type="text/javascript">
function DepartmentSelect(itemToSelect)
{
// Get a reference to the drop-down
var myDropdownList = document.inputForm.department;

// Loop through all the items
for (iLoop = 0; iLoop< myDropdownList.options.length; iLoop++)
{    
    if (myDropdownList.options[iLoop].value == itemToSelect)
     {
       // Item is found. Set its selected property, and exit the loop
       myDropdownList.options[iLoop].selected = true;
       break;
     }
 }
}
</script>

I have verified multiple times that all of the values are good, but for some reason the combobox refuses to show the right value. Any ideas?

Edit: The SCRIPT block occurs within a php echo, so it is pulling the correct value.

Answers


You need to add quotes.

Your code would output:

DepartmentSelect(Deli)

Rather do this:

<SCRIPT LANGUAGE='javascript'>
DepartmentSelect('<?=$row["rDepartment"];?>');
</SCRIPT>

try:

<SCRIPT LANGUAGE='javascript'>
DepartmentSelect(<?php echo $row[rDepartment]; ?>);
</SCRIPT>

or:

<SCRIPT LANGUAGE='javascript'>
DepartmentSelect(<?php echo $row["rDepartment"]; ?>);
</SCRIPT>

Need Your Help

iOS - XML to NSString conversion

ios xml xcode nsxmlparser nsstringencoding

I'm using NSXMLParser for parsing XML to my app and having a problem with the encoding type. For example, here is one of the feeds coming in. It looks similar to this"