Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a dropdown list, that when changed passes the value to a controller.

This controller, through the model then runs a query that returns multiple results.

I need to somehow populate my dropdown list with the results of the query.

my syntax is:

Controller

  function GetDeliveryAddressfromCustomer(){

    $this->load->model('Sales_model');
    if (isset($_POST['data'])){
        $q = strtolower($_POST['data']);
        $data = $this->Sales_model->GetDeliveryAddressfromCustomer($q);
        $this->output->set_content_type('application/json')->set_output(json_encode($data));
    }
}

Model

  function GetDeliveryAddressfromCustomer($q){
    $this->db->select('deliveryaddress');
    $this->db->where('Account', $q);
    $query = $this->db->get('Client');
    if($query->num_rows > 0){
        foreach ($query->result_array() as $row){
            $row_set[] = htmlentities(stripslashes($row['deliveryaddress'])); 
       }
       return $row_set;
    }
  }

View JS

 $.post('GetDeliveryAddressfromCustomer', {data:selectedObj.value},function(result) { 
  var selcust = selectedObj.value;

  //what to put here to pupulate dropdown list

  }); 

View select

 <select id="deliveryaddress" name="deliveryaddress" data-mini="true" data-theme="a">
   <option value="0"></option>
 </select>

the error is: Trying to get property of non-object

I presume the error is from the multiple results being returned. selectedObj.value is an array not a value.

I am less worried about the part of pupulating the dropdown but if you have advice please provide it.

Thanks, Ryan

share|improve this question
is that the complete error? no line number? – bottleboot Jul 26 at 12:58

1 Answer

up vote 1 down vote accepted

please post your json object as i dont know what your data output look like

but anyhow check this out

$.post('GetDeliveryAddressfromCustomer', {data:selectedObj.value},function(result) { 
  var selcust = selectedObj.value;

  //what to put here to pupulate dropdown list
 $("#deliveryaddress").html('<option value=""></option>'); //or you could empty it or ignore this line
$.each(result, function(index, value) {
  $("#deliveryaddress").append('<option value="'+value+'">'+value+'</option>')
            });
        }
        , "json"  //dont forget this
                );
share|improve this answer
Hi Ahmed. This works great, thank you! – Smudger Jul 29 at 7:35
you are welcome :) – Ahmed D. Sherif Jul 29 at 8:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.