0

I Need help on how can i get values of second select box based on first select box

This is view:

$(document).ready(function() {

$('#state').change(function() {
// Get an instance of the select, and it's value.
    var state = $(this),
    stateID = state.val();
// Add if statement to check if the new state id
// is different to the last to prevent loading the same
// data again.

// Do the Ajax request.
$.ajax({
    url : 'http://localhost/ci_ajax/select/get_cities/'+stateID, // Where to.
    dataType : 'json', // Return type.
    success : function(data) { // Success :)
        // Ensure we have data first.
        if(data && data.Cities) {
            // Remove all existing options first.
            state.find('option').remove();
            // Loop through each city in the returned array.
            for(var i = 0; i <= data.Cities.length; i++) {
                // Add the city option.
                state.append($('option').attr({
                    value : data.Cities[i].value
                }).text(data.Cities[i].city));
            }
        }
    },
    error : function() {
        // Do something when an error happens?
    }
});

}); });

<form action="" method="post">
<select name="state" id="state">
    <option>Select</option>
    <?php foreach($states as $row):?>
        <option value="<?php echo $row->id?>"><?php echo $row->states;?></option>
    <?php endforeach;?>
</select>
<select id="cities" name="cities">
    <option>Select</option>
</select>

This is controller:

class Select extends CI_Controller{

function index(){

    $data['states']=$this->load_state();
    $this->load->view('form',$data);
}

function load_state(){

    $this->load->model('data_model');

    $data['states']=$this->data_model->getall_states();

    return $data['states'];
}

function get_cities() {
     // Load your model.
    $this->load->model('data_model');
    // Get the data.
    $cities = $this->data_model->get_cities();
    // Specify that we're returning JSON.
    header('content-type: application/json');
    // Return a JSON string with the cities in.
    return json_encode(array('Cities' => $cities));
}

}

This is model:

class Data_model extends CI_Model{

function getall_states(){

    $query=$this->db->get('states');
    if($query->num_rows()>0){
        foreach($query->result() as $row){
            $data[]=$row;
        }
        return $data;
    }

}

function get_cities(){

    $this->db->select('id,cities');
    $this->db->from('cities');
    $this->db->where('s_id',$this->uri->segment(3));
    $query=$this->db->get();

    if($query->num_rows()>0){
        foreach($query->result() as $row){
            $data[]=$row;
        }
        return $data;
    }

}

}

Please help on this hopefully provide the correct code.

9
  • What error are you seeing? I notice that you're removing/appending the options to the "state" select, rather than the "cities" select, could that be your issue? Commented Jun 7, 2013 at 14:11
  • Do you get any errors which would help... that's a lot of code to look over considering how simple your question is Commented Jun 7, 2013 at 14:12
  • not getting any error just blank array Commented Jun 7, 2013 at 14:13
  • Could any provide me full source code for such process Commented Jun 7, 2013 at 14:14
  • 1
    At what point is this failing? Are you able to populate the first select box? Are you able to send the selected "state" back to your PHP script through AJAX? Are you getting any values back when you run the query to get the cities? Commented Jun 7, 2013 at 14:18

1 Answer 1

0

Because you are accessing the get_cities() function directly, rather than from another function in the controller, your return statement will not actually print the json array to the page.

return json_encode(array('Cities' => $cities));

There are 3 ways to print it: the first is to print or echo the json array (bad practice), the second is to use a view that prints the raw text sent to it. I.E.

$this->load->view('raw', array('data' => json_encode(array('Cities' => $cities)));

With raw.php being just:

<?php print $data; ?>

Or finally you can use the set_output() function in the output class like this:

$this->output->set_output(array('data' => json_encode(array('Cities' => $cities)));

You may also want to make your function load_state() private if it is only going to be accessed from the index() function.

You may have other problems with your code but that is the most obvious one.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.