我如何从CodeIgniter中的选择选项获取文本而不是值

我如何从CodeIgniter中的选择选项获取文本而不是值

问题描述:

I have a form in which there is a select field with many options. These options have numeric values and a text between option tags. What I want to do is to get the text instead of values when I post the form via submit button.

View that generates the select field:

echo form_dropdown('pos', $pos_options, '100', 'id="pos_select" class="form_input"'); 

the select part generated:

<select name="pos" id="pos_select" class="form_input">
    <option value="0">about_history_title</option>
    <option vlaue="1">about_history</option>
</select>

When the form is submited, the appropriate model is called via the controller(text.php):

text.php -> insert_text()

$this->admin_text_model->insert_text();

admin_text_model.php

public function insert_text()
{
    $data = array(
        'tx_page' => $this->input->post('page'),
        'tx_pos' => $this->input->post('pos'),
        'tx_body' => $this->input->post('add_text')
    );

    return $this->db->insert('text', $data);
}

Is there a way that I can get the text inside option tags in $this->input->post('pos')?

Perhaps something like:

'tx_pos' => $this->input->post('pos')->text?

我有一个表单,其中有一个包含许多选项的select字段。 这些选项具有数值和选项标记之间的文本。 我想要做的是在通过提交按钮发布表单时获取文本而不是值。 p>

生成选择字段的视图: p>

  echo form_dropdown('pos',$ pos_options,'100','id =“pos_select”class =“form_input”');  
  code>  pre> 
 
 

生成的选择部分: p>

 &lt; select name =“pos”id =“pos_select”class  =“form_input”&gt; 
&lt; option value =“0”&gt; about_history_title&lt; / option&gt; 
&lt; option vlaue =“1”&gt; about_history&lt; / option&gt; 
&lt; / select&gt; 
  代码>  pre> 
 
 

当表单被提交时,通过控制器调用相应的模型(text.php): p>

text.php - &gt; insert_text() P>

  $这 - &GT; admin_text_model-&GT; insert_text(); 
 代码>  PRE> 
 
 

admin_text_model.php p>

  public function insert_text()
 {
 $ data = array(
'tx_page'=&gt; $ this-&gt; input-&gt; post('page  '),
'tx_pos'=&gt; $ this-&gt; input-&gt; post('pos'),
'tx_body'=&gt; $ this-&gt; input-&gt; post('add_text')  
); 
 
返回$ this-&gt; db-&gt; insert('text',$ data); 
} 
  code>  pre> 
 
 

是否有 我可以在$ this-&gt; input-&gt; post('pos')中的选项标签内获取文字的方式? p>

也许类似于: p> \ n

 'tx_pos'=&gt;  $ this-&gt; input-&gt; post('pos') - &gt; text?
  code>  pre> 
  div>

Try this don't assign the value to option tag:

<select name="pos" id="pos_select" class="form_input">
    <option>about_history_title</option>
    <option>about_history</option>
</select>

It will return text of option.

No, text inside <option> tags is never submitted. That is just how HTML works.

You must set the values, that you need to retrieve, in the value attribute. That is what they are for.

  1. You could make the select yourself and not using the CI helper. This way you have full control on what and where to output it:

    <select name="pos" id="pos_select" class="form_input">
      <?php foreach($pos_options as $option):?>
        <option value="<?php echo htmlentities($option);?>"><?php echo $option;?></option>
      <?php endforeach;?>
    </select>
    
  2. Or, you could make an ugly hack in javascript:

    $('#pos_select option').each(function(){
       $(this).attr('value', $(this).text()); 
    });
    

Which sets the value of every option equal to its text content: http://jsfiddle.net/ursQY/