title: Updating list using AJAX in Codeigniter (within a form)
body: <p>I have a form that adds clients to a database <code>clients/add_client</code>. The <code>add_client</code> method has a parameter of <code>$state</code> which is passed to another method <code>list_centers()</code>. When someone is talking to a potential client, they have all of our centers in a sidebar. There is a <code>&lt;select&gt;</code> above the list of centers in which lists the states where we have centers. When they change the select to another state, it should list all of the centers in that state. Now, I have this working by passing the parameter in the URL like this: <code>localhost/clients/add_clients/GA</code> lists all of the centers in Georgia. The problem is that I want to do this with AJAX and not have the page refresh. I cannot figure out how to pass this data via ajax. I know that I have to reconstruct the list each time but I am stuck. Here is what I have tried:</p>
  
  <pre><code>$('#center_select').change(function(){
          var data = $(this).val();
          var url = 'add_client/' + data;
          $.ajax({
              type: 'POST',
              dataType: 'html',
              data: data,
              url: url,
              success: function(data) {
                  console.log(data);
              },
              error: function(data) {
                  console.log(data);  
              } 
          });
          return false;
      });
  </code></pre>
  
  <p>Just in case you need the method:</p>
  
  <pre><code>public function add_client($state = false) {
  
          $this-&gt;load-&gt;model('centers_model');
  
          $data = array(
              'page_title' =&gt; 'Add Client',
              'client_status' =&gt; $this-&gt;clients_model-&gt;list_client_status(),
              'centers' =&gt; $this-&gt;centers_model-&gt;list_centers(null, $state),
              'center_states' =&gt; $this-&gt;centers_model-&gt;list_center_states(),
          );
  
          $this-&gt;load-&gt;view('header');
          $this-&gt;load-&gt;view('clients/add_client', $data);
          $this-&gt;load-&gt;view('footer');
  
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;div class="col-sm-3"&gt;
              &lt;aside id="centers_sidebar" class="well"&gt;
                  &lt;h2&gt;List of Centers&lt;/h2&gt;
                  &lt;select class="form-control" name="center_select" id="center_select"&gt;
                      &lt;option value="all"&gt;All&lt;/option&gt;
                      &lt;?php
                          foreach ($center_states as $center_state) {
                              echo '&lt;option value="' . $center_state-&gt;center_state . '"&gt;' . $center_state-&gt;name . '&lt;/option&gt;';
                          }
                      ?&gt;
                  &lt;/select&gt;
                  &lt;ul id="center_list"&gt;
                      &lt;?php
                          foreach ($centers as $center) {
                              $output = '&lt;li class="center"&gt;';
                              $output .= '&lt;h5&gt;' . $center-&gt;center_name . '&lt;/h5&gt;';
                              $output .= '&lt;p&gt;' . $center-&gt;center_type . '&lt;/p&gt;';
                              $output .= '&lt;p&gt;' . $center-&gt;center_city . ', ' . $center-&gt;center_state . '&lt;/p&gt;';
                              $output .= '&lt;p&gt;' . $center-&gt;center_phone . '&lt;/p&gt;';
                              $output .= '&lt;/li&gt;';
                              $output .= '&lt;hr&gt;';
  
                              echo $output;
                          }
                      ?&gt;
                  &lt;/ul&gt;   
              &lt;/aside&gt;
          &lt;/div&gt;
  </code></pre>