Search code examples
phpjquerydropdownblueimp

With jQuery Multiple File Upload how do you populate a value in a dropdown list so that an item shows as selected?


I have the program set up to save values into a database - this works fine. For each image uploaded I want to assign it to a category from a dropdown list of options. jmfu uses syntax like

{%=file.id||''%} # to get the ID
{%=file.name%} # to get the filename
{%=file.category||''%} # to get/display the category

To get the value into a format so that I can get the category value out of the database and do an if test to see if the dropdown option is selected I added this code:

$category = "{%=file.category||''%}";

Then the dropdown code:

Category <select name="category" id="{%=file.id||''%}" size="1" class="saveData set-field-width">
    <option value=""  <?php if ( $category == "" ) { echo " selected "; } ?> >-- Please select --</option>
    <option value="Carpet" <?php if ( $category == "Carpet" ) { echo " selected "; } ?> >Carpet</option>
    <option value="Tile" <?php if ( $category == "Tile" ) { echo " selected "; } ?> >Tile </option>
</select>

If I add an echo statement to see what is happening:

echo "category: $category | <br />";

It displays the correct value to the screen, but viewing the source via the browser it displays the category value as:

{%=file.category||''%}

rather than Carpet or Tile which is why 'selected' will not work. I tried setting variables for both $file_id and $filename but get the same problem:

$file_id = {%=file.id||''%};
$filename = {%=file.name%};

The php if statement will never be successful, as is:

because $category as seen in HTML source view is: {%=file.category||''%} rather than Tile or Carpet. Is there a way to convert/translate the coded values so that they work and select correctly?

A demo can be seen at: http://www.dottedi.biz/demo/code/public/jquery-file-upload

The image shown here shows the html source view and the problem.

HTML Source View


Solution

  • Replace this: <?php if ( $category == "Tile" ) { echo " selected "; } ?> with this: {% if (file.category == "Tile") { %} selected {% } %}. See the below link for more information: https://blueimp.github.io/JavaScript-Templates/