Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+1 vote
924 views
in Q2A Core by

I have a select in the form like this:

<select name='select1' ...>

<option>(null)</option>

<option>test1</option>

<option>test2</option>

</select>

Now if i select "test1" and submit form. qa_post_text('select1') would be 1 rather than "test1" but I want get the text displayed on the screen not the index of the selected item. How can I achieve this?

 

PS: I hacked the qa-theme-base.php and make a change like this:

        function form_select($field, $style)
        {
            $this->output('<select '.@$field['tags'].' class="qa-form-'.$style.'-select">');
            
            foreach ($field['options'] as $tag => $value)
                $this->output('<option value="'.$value.'"'.(($value==@$field['value']) ? ' selected' : '').'>'.$value.'</option>');
            
            $this->output('</select>');
        }

And now i can get the text value "test1" using qa_post_text('select1').

But I don't wanna change the core code, is there any other way to do this?

Q2A version: 1.6.3

3 Answers

+1 vote
by
 
Best answer
I just found out that I have to store my options array outside the form part like this:

$options= load_drom_db(...);
$option_key = qa_post_text('select');
$option_value = $options[$option_key];

and then I can set like this :

'select' => array(
                        'label' => ...,
                        'tags' => 'name="select" id="select"',
                        'type' => 'select',
                        'options' => $options,
                        'value' => $option_value,),...

 

and then it will work fine...

 

Hope this will help q2a rookies like me...
0 votes
by

I stumbled over this problem as well. "value" is misleading here, as it means the "label" of the select, not the value of the select.

Without a core hack you just do:

    $options = {
       'value one' => 'item label one',
       'value two' => 'item label two',
    }

     $fields[] = array(
          'type' => 'select',
          'label' => 'This is my label',
          'tags' => 'name="qa_id_name_to_be_read_on_post"',
          'options' => $options,
          'value' => $options[ 'item label two' ], // <select>, value must not be "option value" but option name
       );

 

Have not tested the code above but should work :)

by
I can see what you did here. Kind of reverse the key and value. But this might be confusing if someone else read my code. My solution is use variables to store the selected key and value outside. Not sure if this is good coding style but it works.
0 votes
by

To get selected value of option you need to set 'value' key with qa_opt('your_option_field_key_here') and you do not need any core hack...

@q2apro.com

Just to corss confirm, if you set 'value' with direct array key than it will always keep selected that item regardless what you have stored in database.

Here is some example: This is for plugin option but the value you can

// set levels options
$levels = array('Expers', 'Editors', 'Moderators');

// form field
array(
    'label' => 'Select Level',
    'tags' => 'NAME="level_item"',
    'id' => 'level_item',
    'type' => 'select',
    'options' => $levels,
    'value' => qa_opt('level_item'), //keep store item selected
),

// set defautl option
function option_default($option) {
    switch($option) {
        case 'level_item':
            return 'Editors';        
        default:
            return null;
    }   
}

You can get any option of the system either from Admin section or your plugin option using qa_opt()

by
Thank you for answering.

My options are like this :
$options = array(
  0 => "option1"
  1 => "option2"
  2 => "option3"
);

if i select option2, echo qa_opt("my_select_name");
it will print out 1 rather than "option2" but I wanna get the text of "option2".
So my solution is store them outside qa_content like this:
$option_key = qa_post_text("my_select_name");
$option_value = $options[$option_key];
and set value => $option_value

and this works for my code anyway...
...