Rajan
Rajan

Reputation: 2425

Selected as Selected Database value Codeigniter

In user edit module have a dropdown in which i show options from database.

Now i select a value from dropdown and want to set it as selected. SO next time when i edit user i can see which was the last value selected.

When i edit a user i select some value from dropdown, and then save the record back to database. Next time when i open that record i want the option i selected last time to be shown selected.

I tried this :

<tr>
    <td>Base INI File</td>
      <?php 

    if(isset($_GET['id'])) 
    { 
    $id=$_GET['id']; 
    btn_edit_file($id); 
    } 
    ?>  
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 


    <td> 
        <select required name="base_ini_id" id="base_ini_id" class="form-control"> 

        <option value="">Select</option> 
        <?php foreach($base as $value) { ?> 
        <option id="emp" class="specialLink" value="<?php echo $value->id;?>"><?php echo $value->base_ini_filename;
          if($value->id == $value->base_ini_filename){echo "selected='selected'";}  ?> </option> 
        <?php } ?> 
        </select> 
    </td> 


    <td> 

    <?php echo btn_edit('customer/upload_ini/edit_ini_custom/'); ?> 
    </td> 


    <script type="text/javascript"> 
            $(document).ready(function() { 
                $('#base_ini_id').change(function() { 
                    var id = $("#base_ini_id").val();
                    var url = "/bizrtc/customer/upload_ini/edit_ini_custom/";

                    $("#edit_link").attr("href",url+ id); 
                    $("#edit_link").attr("target","_blank");

                }); 
            }); 
        </script>
     </tr>

Upvotes: 1

Views: 3002

Answers (3)

Dray
Dray

Reputation: 887

do this :

<select required name="base_ini_id" id="base_ini_id" class="form-control"> 

    <option value="">Select</option> 
    <?php foreach($base as $value) 
    { 
      ?> 

    <option id="emp" class="specialLink" value="<?php echo $value->id;?>" 
      <?php if($value->id == $user->base_ini_id){echo "selected";} ?>>
      <?php echo $value->base_ini_filename;?></option>

    <?php } ?> 

    </select> 

Upvotes: 1

Subash
Subash

Reputation: 230

where is the user saved value ?

first you should get the user saved value into one variable

you option tag should look like below

    <option id="emp" class="specialLink" value="<?php echo $value->id;?>">
   <?php
    if($value->id == $usersavedvalue){echo "selected='selected'";}  ?> ><?php echo $value->base_ini_filename; ?> </option> 

Upvotes: -1

Ceeee
Ceeee

Reputation: 1442

Try below code:

<select required name="base_ini_id" id="base_ini_id" class="form-control"> 
    <option value="">Select</option> 
    <?php foreach($base as $value) { ?> 
            <option id="emp" class="specialLink" value="<?php echo $value->id;?>" <?php if($value->id == $value->base_ini_filename){echo "selected='selected'";} ?> >
                <?php echo $value->base_ini_filename; ?>
            </option> 
    <?php } ?> 
</select> 

Aside from the incorrect closing tag in HTML, I also would like to note that you might want to double check your condition:

if($value->id == $value->base_ini_filename)

Seems like you are comparing very different thing

Upvotes: 1

Related Questions