title: Codeigniter, jQuery and Ajax - Preventing Page Refresh on Displaying New Database Content Dynamically
body: <p>I'm trying to achieve something relatively straightforward - allow users to make comments on a  view page, update the database and display the new content dynamically on the page without the whole page refreshing.</p>
  
  <p>My efforts so far have been unsuccessful - there may be several issues with what I'm doing! </p>
  
  <p>On the view page I have tried this (to send 2 variables to a CI controller function):</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function ajax(id, user_id) {
  jQuery('#dynamicdiv').load('/controller/function/' + id + '/' + user_id);
  }
  &lt;/script&gt;
  
  &lt;div id='dynamicdiv'&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I have a textarea to collect the user comment, in the controller function should I be able to call this as post data in order to write it to the database? If so, I would still need to send two other variables ($id and $user_id) to the ajax function.</p>
  
  <pre><code>&lt;?php $atts = array (
    'class' =&gt; "alignright button",
    'onClick' =&gt; "ajax('$id,$user_id')",
    ); ?&gt;
  &lt;?php echo anchor(current_url(), 'Post my Comment', $atts); ?&gt;
  </code></pre>
  
  <p>and in the controller, which involves a different function (view) than the page I want the user to stay on:</p>
  
  <pre><code>$data = $this-&gt;model-&gt;database_query($id, $user_id);
  
  echo $data; // from reading other posts it seems I would process the data within this function, to have it displayed on the page the user is viewing?
  </code></pre>
  
  <p>Any help appreciated!</p>