1

I can't get the text in form_dropdown it always return the option value. Please help.

view

<?php echo form_open_multipart('upload/do_upload');?>
<?php 
    $cont ='';
    $dept='';
    foreach($level->result() as $row) {
        $cont = $row->userlevel;
        $dept = $row->department;
    }
    if(strtoupper($cont)=="USER") {
        echo "<strong>".$dept."&emsp;</strong>";
    }
    else {
        echo form_dropdown('department_name', $dept_name,'','id="department_name"');
    } 
?>
<br/><br/>
<div class="btn-group">
    <span id="status2"><?php echo form_dropdown('document_name', $document_name, '', 'id="document_name"'); ?></span>
</div>
<br/><br/>
<label for="file">Select File To Upload:</label>
<input type="file" name="userfile" multiple class="btn btn-file"/>
</br></br>
<input type="submit" value="Upload File" class="btn btn-primary"/>
<?php echo form_close(); ?>

<script>
    window.onload = function() {
        var form_data = {
            dept_name: "<?php echo $dept; ?>",
            ajax: 1
        };
        $.ajax({
            url:"<?php echo site_url("document/document_dept_received"); ?>",
            data:form_data,
            type:'POST',
            success: function(msg){
                document.getElementById("status2").innerHTML = msg;
            }
        });
    };

    $("#department_name").change(function() {
        var form_data = {
            dept_name: $("#department_name option:selected").text(),
            ajax: 1
        };
        $.ajax({
            url:"<?php echo site_url("document/document_dept_received"); ?>",
            data:form_data,
            type:'POST',
            success: function(msg){
                document.getElementById("status2").innerHTML = msg;
            }
        });
    });
</script>

controller

$upload_data = $this->upload->data();
$data['thumbnail_name'] = $upload_data['raw_name']. '_thumb' .$upload_data['file_ext'];
$file_array = array(
    'image'         => $data['thumbnail_name'],
    'image_name'    => $upload_data['file_name'],
    //'description'   => "",
    'date_created'  => date('Y-m-d H:i:s', now()),
    'date_modified' => date('Y-m-d H:i:s', now()),
    'author'        => $this->session->userdata('username'),
    'size'          => $upload_data['file_size'],
    'type'          => $upload_data['image_type'],
    'width'         => $upload_data['image_width'],
    'height'        => $upload_data['image_height'],
    'document_name' => $this->input->post("document_name"),
    'department'    => $this->input->post("department_name"),
    //'notes'         => "",
);

when I try this...

print_r($this->input->post("document_name"));
print_r($this->input->post("department_name"));

it shows 1 and 1...

what I want is the text not the id or value of options. Example: I selected the IT Department and Folder 1, it will display/record IT Department and Folder 1 to database.

jned29
  • 477
  • 12
  • 50
  • check this: http://stackoverflow.com/questions/610336/javascript-retrieving-the-text-of-the-selected-option-in-select-element – vaso123 Oct 20 '14 at 09:16
  • I tried to check that. The problem is that I'm using form_dropdown which is connected to database. – jned29 Oct 20 '14 at 09:19
  • So then you need to create your array as a key/value array, and make the key as a text not an integer. – vaso123 Oct 20 '14 at 09:22
  • But if you want to use integer as an option value, see the link what i commented. That is exactly what you want, that is get out the selected options text, not the option value. – vaso123 Oct 20 '14 at 09:24
  • no. I want text not integer. I tried to review the link before but not working. see my code I used ajax jquery and I got the text and since I'm using form_open_multipart, the script cannot be applicable to get the text value. – jned29 Oct 20 '14 at 09:30
  • no. I want text not integer. I tried to review the link before but not working. see my code I used ajax jquery and I got the text and since I'm using form_open_multipart, the script cannot be applicable to get the text value – jned29 Oct 21 '14 at 01:09

2 Answers2

0

Exchange array values and keys:

$dept_name = array_flip($dept_name);

form_dropdown('department_name', $dept_name,'','id="department_name"');

Same for $document_name.

This switches from <option value="id">name</option> to <option value="name">id</option>.


If you need <option value="name">name</option>, rebuild the array to get a name 2 name relationship.

$dept_names2 = array();

foreach($dept_name as $item_id => $item_name)
{
    $dept_names2[$item_name] = $item_name; // name as key and value
}

form_dropdown('department_name', $dept_names2,'','id="department_name"');

An array with structure "name=>name" will work with your ajax requests, too - because you are using $("#department_name option:selected").text(). so you are already working with the names, not the ids. no adjustment needed.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

Check this: http://jsfiddle.net/Lev0sjjx/2/

Note: The jquery code is just for demonstration purposes

HTML

<select id="test">
    <option value="1">Test One</option>
    <option value="2">Test Two</option>
</select>

Javascript / jQuery

function getSelectedText(elementId) {
    var elt = document.getElementById(elementId);

    if (elt.selectedIndex == -1)
        return null;

    return elt.options[elt.selectedIndex].text;
}

$(function() {
    $('#test').change(function() {
        var text = getSelectedText('test');
        alert(text);
    });
});
vaso123
  • 12,347
  • 4
  • 34
  • 64
  • 1
    He has that already: `dept_name: $("#department_name option:selected").text(),`. But this part is not related to the form submit. – Jens A. Koch Oct 20 '14 at 09:38
  • But he want to send it bye ajax, as i understood. – vaso123 Oct 20 '14 at 09:40
  • I'm using form_open_multipart() and form_dropdown() – jned29 Oct 20 '14 at 09:40
  • Oh :) and i thought he want to send via form submit button .. hehe – Jens A. Koch Oct 20 '14 at 09:40
  • form_open_multipart doesn't matter, since, you want to get text bye javascript, and then send with ajax – vaso123 Oct 20 '14 at 09:41
  • i used ajax only when I selected IT department it will only shows document under that department, what I'm doing is to insert the selected option to database `` – jned29 Oct 20 '14 at 09:42
  • @jens, yes I want to send via form submit button. because my ajax on for viewing only not for saving the selected options to database – jned29 Oct 20 '14 at 09:43
  • then you need to build your array like this: `$dept_name = array('IT Department' => 'IT Department');` Through `$_POST` you will get back always the value of the option. – vaso123 Oct 20 '14 at 10:25