Note that there are some explanatory texts on larger screens.

plurals
  1. PODropdown population using jquery to get data from codeigniter controller
    text
    copied!<p>I want to populate a dropdown using jquery and ajax.</p> <p>my jquery call is </p> <pre><code>&lt;script&gt; $(document).ready(function(){ $.ajax({ type: "GET", url: "&lt;?=base_url()?&gt;user/getstate/", //the script to call to get data data:'', //you can insert url argumnets here to pass to api.php dataType: 'json', //data format success: function(data){ //on recieve of reply $("#state").append('&lt;option selected&gt;State&lt;/option&gt;'); for(i in data) $("#state").append("&lt;option value=\""+data[i][0]+"\"&gt;"+data[i][1]+"&lt;/option&gt;"); } }); }); &lt;/script&gt; </code></pre> <p>the part that needs to be populated is </p> <pre><code>&lt;select id="state" class="span2" style="height:30px" name="state" required&gt; &lt;/select&gt; </code></pre> <p>the function in the controller is </p> <pre><code>public function getstate(){ $this-&gt;load-&gt;model('usermodel'); $data=$this-&gt;usermodel-&gt;getstate(); echo json_encode($data); } </code></pre> <p>the model from which the data is being fetched is </p> <pre><code>public function getstate(){ $sql="SELECT * FROM statelist"; $query=$this-&gt;db-&gt;query($sql); $result=$query-&gt;result_array(); return $result; } </code></pre> <p>the datas in statelist are </p> <p>sid | statename</p> <p>wb | West Bengal</p> <p>ga | Goa</p> <p>The problem i am facing is that when I load the page I get the dropdown with 2 elements as expected but in this form</p> <p>State <br/> undefined <br/> undefined</p> <p>Now i don't understand why am i getting "undefined" instead of the required or expected data like</p> <p>State <br/> West Bengal <br/> Goa</p> <p>A solution will really help me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload