kapantzak
kapantzak

Reputation: 11750

How I get the text instead value from a select option in 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?

Upvotes: 2

Views: 10156

Answers (3)

Damien Pirsy
Damien Pirsy

Reputation: 25445

  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/

Upvotes: 0

Jitendra Yadav
Jitendra Yadav

Reputation: 896

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.

Upvotes: 1

Sverri M. Olsen
Sverri M. Olsen

Reputation: 13283

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.

Upvotes: 4

Related Questions