Categories > TinyButStrong general >

Setting selected item out of html SELECT list based on database value

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: joshhendo
Date: 2010-07-15
Time: 06:01

Setting selected item out of html SELECT list based on database value

Hi,

I am trying to make a page which allows a user to edit the database. Obviously when they edit a page, all the current values need to be entered into the fields so they can edit only parts of it.

Currently, when creating a new database entry, I have the following:

<select name="category">
    <option value="-1">No Category</option>
    <option value="[blkCategories.CategoryID]">[blkCategories.Name;block=option]</option>
</select><br>

That lists all the categories I have stored in my $categories array with no problems, and works quite well. The problem that I have is when editing, I want the value that is in the database (which I have stored in a variable, say $currentCategoryID). I want to have that option value selected by default (with selected="selected" I presume).

I have though about having another option display just below "No Category" which will supposedly display the category name and have it selected by default, but this seems very hackish and won't work as I want for things such as the date field (which is split up into day and month drop down menus)

<select name="category">
  <option value="-1">No Category</option>
  <option value="[blkCategories.CategoryID]" selected="selected">[blkCategories.Name;block=option][onload;block=option;when [blkCategories.CategoryID]=[blkJobDetails.CategoryID]</option>
  <option value="[blkCategories.CategoryID]">[blkCategories.Name;block=option]</option>
</select><br>

I actually have no idea if that WOULD work (I haven't bothered testing it because I don't think it will work, and even IF it does, it isn't what I want and is very hackish). If I did it that way, the current category would ALWAYS appear as the second value (after No Category), and the second value is always the one that will be selected. Then afterwards it will list all the available categories (including the one already selected, therefore listing it twice.) Rather than having it done this way, it would be better if it was selected out of the list already available.

Any advice is appreciated.

Regards,
Josh
By: TomH
Date: 2010-07-15
Time: 08:35

Re: Setting selected item out of html SELECT list based on database value

Please review the working examples at
http://www.tinybutstrong.com/examples.php

See "Plug in: Selecting items" near the bottom of the available examples

More than enough  variations with the source code available there.

If you are new to TBS then it will benefit you greatly to review and apply each of the examples to your own situation - it will greatly improve your understanding of how to use TBS.
By: joshhendo
Date: 2010-07-15
Time: 15:51

Re: Setting selected item out of html SELECT list based on database value

Hi,

Thanks for that! I managed to get it working after playing around with it, learning something new everyday!

I am relatively new to TBS, but I have found it to be great, though I hadn't yet looked into any of the plugins (obviously.)

Thanks for your help

Josh.