Welcome Guest, Not a member yet? Register   Sign In
Is there a way I can auto fill a text area based on a selectbox drop down?
#1

I'm trying to auto fill a textarea based on the selection of a selectbox.

I want the auto fill text to come from my database and it would be nice if the option names in the selectbox could come from the database also, but not necessary - they can be static.

I'm sure I will have to use some kind of JavaScript or Json, etc. Any help you can give I appreciate it.
Reply
#2

(This post was last modified: 11-19-2014, 04:22 PM by Rufnex.)

Ok, lets think about that your database have a table with the otpions and the related information (title, information).

Make a model with a method that gives you back the table data. Call it form the controller and route it to the view. In the view loop over the data an build the option fields.

Thats for the part 1. If the information you want to put into the textarea is not to long you can do it directyl. otherwise it could be an idea to do it with an ajax request. I would recommend to use jQuery. Look at the following untested example as a kind of blueprint.

Model
PHP Code:
public function get_options()
    {
        
$query $this->db->get('options');
        if(
$query->num_rows() > 0)
        {
            return 
$query->result_array();
        }
        return 
false;
    } 

Controller
PHP Code:
public function index()
    {
        
$this->data['options'] = $this->your_model->get_options();
        
$this->load->view('example'$this->data);
    } 

View
PHP Code:
<select id="example">
    <?
php foreach($optionsas $option): ?>
    <option data-info="<?php echo $option['information']; ?>"><?php echo $option['title']; ?></option>
    <?php endforeach; ?>
</select>

<textarea id="info"></textarea>

<script src="...jquery..."></sript>
<script>
$(document).ready(function() {
    $('#example').change(function(){
        $('#info').val( $(this).find('option:selected').data('info') ); 
    });
});
</script> 

If it is ok by that way fine .. otherwise you have to user ajax .. let me here ^^.

Reply
#3

Thanks, I'll try this
Reply




Theme © iAndrew 2016 - Forum software by © MyBB