PDA

View Full Version : Each From with Select Option


grey_goose
05-27-2017, 01:39 PM
field298 Single Line Text Box

Instead of free input I wanted to show a select dropdown to validate input. There were a ton of options, so I created an array, and then looped through the array to create the <options>. A first blank option was also added.

Template

<select class="primary" name="userfield[field298]" id="cfield_298" tabindex="1">
<option value="" <vb:if condition="$bbuserinfo['field298'] == ''">selected="selected"</vb:if> ></option>
<vb:each from="titlearray[$allcharacterdb[$bbuserinfo['field109']]['Organization']]" key="userid" value="userinfo">
<option value="{vb:raw userinfo.title}" <vb:if condition="$bbuserinfo['field298'] == '{vb:raw userinfo.title}'">selected="selected"</vb:if> >{vb:raw userinfo.title}</option>
</vb:each>
</select>


HTML Output (small sampling)

<select class="primary" name="userfield[field298]" id="cfield_298" tabindex="1">
<option value=""></option>
<option value="Adept">Adept</option>
<option value="Axesworn">Axesworn</option>
</select>


So this all works, and saves fine. The problem is that the selected attribute isn't catching on the option/field after it's been saved and then the form is brought up again. So the next time the form pulls up, it's blank, and then clears the field. Here's where I'm trying to set the attribute. What am I missing?

<vb:if condition="$bbuserinfo['field298'] == '{vb:raw userinfo.title}'">selected="selected"</vb:if>

Dave
05-27-2017, 01:48 PM
You can't use vBulletin template syntax inside of a condition. Now it checks if it literally equals the string '{vb:raw userinfo.title}'. Changing it to the following should work, given that the $userinfo array variable is available.

<vb:if condition="$bbuserinfo['field298'] == $userinfo['title']">

grey_goose
05-27-2017, 01:54 PM
Ugh, I knew that. Got too dialed in on the array and using options withe a text input to remember the basics.

Thanks so much :)

https://vborg.vbsupport.ru/external/2017/05/3.jpg