title: Parsing JSON data from ajax call
body: <p>I have posted a few questions previously on how to do the same thing, and people have pointed me in the right direction. So my apologies if this is getting repetitive. However I am posting this time to see if someone could spare a couple of seconds to help me with javascript.</p>
  
  <p>I have a form that already submits data into a different table, and want to implement a "pre-canned response feature". This would simply be a drop list selection that when the <code>onChange</code> event fires of an option list it runs the javascript that populates a following text area with appropriate data.</p>
  
  <p>I believe as it is already in a form, I can't simply put it in another form and use submit, also, I want to be able to do this without refreshing the page.`</p>
  
  <pre><code>&lt;form action="&lt;?=base_url();?&gt;ticket/addmessage/&lt;?=$ticket_details['id'];?&gt;/" enctype="multipart/form-data" method="post" name="message"&gt;
  
          &lt;label for="frm_precan"&gt;Canned Response&lt;/label&gt;
          &lt;span class="input"&gt;                        
              &lt;select id="frm_precan" name="precan" onchange="updateText()"&gt;
                  &lt;option value=""&gt;--Please Select--&lt;/option&gt;
                  &lt;?php foreach($precan_list as $precan) :?&gt;
                      &lt;option value="&lt;?=$precan['id']; ?&gt;"&gt;&lt;?=$precan['name'];?&gt;&lt;/option&gt;
                  &lt;?php endforeach; ?&gt;            
              &lt;/select&gt;
          &lt;/span&gt;
  
          &lt;ul&gt;
              &lt;li&gt;&lt;label for="message"&gt;Message&lt;span class="req"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;span class="input"&gt;&lt;/span&gt;&lt;br/&gt;&lt;/li&gt; 
          &lt;/ul&gt;   
          &lt;textarea style="width: 100%; margin: 0; padding: 0; border-width: 1; font-family: courier;" name="message" rows="10" id="text_area"&gt;&lt;/textarea&gt;
          &lt;button type="submit"&gt;&lt;span&gt;Add Message&lt;/span&gt;&lt;/button&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>This is my HTML form. It uses PHP via SQL to populate the option list, as the pre-canned messages are stored in SQL table.</p>
  
  <p>So what I need to do is somehow link this Javascript (that is called on the <code>onChange</code>):</p>
  
  <pre><code>        function updateText()
         {
         var message = document.getElementById('frm_precan').value;
         $('#text_area').val(message)
         };
  </code></pre>
  
  <p>(very basic, I know, but this is where I struggle) So this code wants to pass the <code>('frm_precan').value;</code> (which is the ID in the table, and the field by which I want to query the correct message)... to a php file that looks like this:</p>
  
  <pre><code>    public function get_message($message_id)
      {
          $sql_list  =
          "
          SELECT *
          FROM  ".$this-&gt;tables_automessages."
          WHERE id = '".mysql_real_escape_string($message_id)."'";
  
          $query = $this-&gt;db-&gt;query($sql_list);
          if($query-&gt;num_rows() &gt; 0)
              {
              foreach ($query-&gt;result_array() as $row)
                  {
                  $return[] =
                  array(
                          'message'                   =&gt;  $row['message']
  
                      );
                  }
              return $return;
              }
          else return false;
          }
  }
  </code></pre>
  
  <p>O, and I am using code ignitor, so this could also be a reason why I am getting confused. So the variable wants to come out of the javascript into a controller then go to the SQL query.</p>
  
  <p>If someone can understand what I mean, I will be amazed... and very grateful.</p>