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

I noticed if we add a select field on registration form, and at submit the form presents some errors (not at select field) the POST value for select field is not maintained.

This is because of an error in form_select:

$this->output('<OPTION VALUE="'.$tag.'"'.(($value==@$field['value']) ? ' SELECTED' : '').'>'.$value.'</OPTION>');

should be:

$this->output('<OPTION VALUE="'.$tag.'"'.(($tag==@$field['value']) ? ' SELECTED' : '').'>'.$value.'</OPTION>');
by
I think a similar bug is in form_select_radio as well.
by
If I change that like I mentioned the some of the administration tools won't work properly.

Sample is: Administration center - General

2 Answers

0 votes
by
edited by
I can see why you'd think that, but actually this is correct. You pass through the submitted value (even if it includes HTML) as the field's value, rather than the key.
by
OK, but for a select field if I pass as options

array(
'US'=>'United States',
'CA'=>'Canada'
)

Seems the value is 'United States' and not 'US' as expected...
by
If I am not wrong according to standards the form POST/GET value is be key (US) and not the label...
by
Yes, but within the 'value' element in Q2A's field arrays you indicate which is selected via the label. It's an internal Q2A design decision, not something there is a standard for. The reason is no maintain consistency with other field types in Q2A, which also use the 'value' for the fully-rendered HTML.
0 votes
by

To show the user the saved option, we're supposed to pass in the value, except that's not the setting that's being returned from qa_post_text. The information being returned qa_post_text is the tag.

Existing code:

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="'.$tag.'"'.(($value==@$field['value']) ? ' SELECTED' : '').'>'.$value.'</OPTION>');
    $this->output('</SELECT>');
}
 

My suggestion:

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="'.$tag.'"'.(($tag==@$field['tag']) ? ' SELECTED' : ''). '>'.$value.'</OPTION>');
    $this->output('</SELECT>');
}

This is important because the tag is typically an ID which doesn't change, while the value could be a user friendly name, which could change.

When the user saves the form, qa_post_text returns the tag of the item selected. It's the tag that's stored in qa_opt, not the value. So to select the correct item in the dropdown, we need to use the tag that we stored in the db.

by
Hi, I got a related question. If I want to get the value (which is probably a text) using qa_post_text, how can I achieve this? Thx.
...