title: Form data not passing to database on clicking save
body: <p>Currently I have an edit page where any changes I make are reflected in my database. But one part of my form is not sending the data which is the notes tab where if there is no notes, the user can add a new note to the database, or if a note already exists, then they should be able to edit it. Currently this is my code:</p>
  <p>View Class:</p>
  <pre><code>&lt;div id=&quot;notes-wrap&quot;&gt;
  &lt;table id=&quot;tableobj1&quot;&gt;
      &lt;thead&gt;
        &lt;tr class=&quot;active&quot;&gt;
          &lt;th&gt;Date&lt;/th&gt;
          &lt;th&gt;Notes&lt;/th&gt;
          &lt;th nowrap&gt;Added By&lt;/th&gt;
          &lt;th colspan=&quot;2&quot; nowrap&gt;Action&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr id=&quot;objrows1&quot; class=&quot;tblerow1&quot;&gt;
          &lt;td colspan=&quot;3&quot;&gt;&lt;textarea name=&quot;note&quot; class=&quot;form-control notes&quot; placeholder=&quot;Notes&quot;&gt;&lt;/textarea&gt;&lt;/td&gt;
          &lt;td colspan=&quot;2&quot; width=&quot;1%&quot;&gt;&lt;button type=&quot;submit&quot; class=&quot;btn-sm savenotes&quot; data-href=&quot;&lt;?php echo site_url('contacts/savenotes/'.$leadid); ?&gt;&quot; &gt;
          &lt;i class=&quot;fa fa-save&quot;&gt;&lt;/i&gt;&lt;/button&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php if($notes) foreach($notes as $val):?&gt;
        &lt;tr&gt;
          &lt;td nowrap=&quot;nowrap&quot;&gt;&lt;?php echo($val['added_date']);?&gt;&lt;/td&gt;
          &lt;td width=&quot;75%&quot; class=&quot;textnote&quot;&gt;&lt;?php echo nl2br($val['note']) ?&gt;&lt;/td&gt;
          &lt;td width=&quot;15%&quot;&gt;&lt;small&gt;&lt;?php echo $val['display_name'] ?&gt;&lt;/small&gt;&lt;/td&gt;
          &lt;td width=&quot;1%&quot; align=&quot;center&quot;&gt;&lt;a&gt;&lt;i class=&quot;fa fa-pencil&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;a data-url=&quot;&lt;?php echo site_url('contacts/updatenote/'.$val['notes_id']) ?&gt;&quot; style=&quot;display:none&quot;&gt;&lt;i class=&quot;fa fa-save&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
          &lt;td width=&quot;1%&quot; valign=&quot;middle&quot; align=&quot;center&quot;&gt;&lt;a data-url=&quot;&lt;?php echo site_url('contacts/deletenote/'.$val['notes_id']) ?&gt;&quot; &gt;&lt;i class=&quot;fa fa-trash&quot;&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;?php endforeach;  ?&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  
  &lt;button type=&quot;submit&quot; class=&quot;btn btn-success waves-light&quot;&gt;Submit&lt;/button&gt;
  </code></pre>
  <p>JS</p>
  <pre><code>$(document).ready(function(){
      $(&quot;#notes-wrap&quot;).on(&quot;click&quot;, &quot;button.savenotes&quot;, function(e){ 
          e.preventDefault();
          saveleadnotes($(this)); 
      });
  
      $(&quot;#tableobj1&quot;).on(&quot;click&quot;, &quot;tbody a.remove&quot;, function(e) {
          var $t=$(this).closest ('tr');$u = $(this).data('url');  
          swal({
              title: &quot;Are you sure?&quot;,
              text: &quot;You will not be able to recover this!&quot;,
              type: &quot;warning&quot;,
              showCancelButton: true,
              confirmButtonClass: 'btn-warning',
              confirmButtonText: &quot;Yes&quot;,
              closeOnConfirm: true
          }, function (e) { 
              if($u &amp;&amp; e){
                  removeobj($u); 
                  $t.remove();
              } 
              else if(e){ 
                  $t.remove(); 
              } 
          }); 
      }); 
  
      function saveleadnotes(obj){ 
          $.ajax({
              url: obj.data('href'),
              async:false,
              type:&quot;post&quot;,
              data:obj.closest('.tblerow1').find(':input').serialize(),
              success: function(result){ 
                  if(result&lt;1)        
                  {
                      obj.closest('.tblerow1').find('[name=&quot;note&quot;]').focus(); 
                  }
                  else {
                      leadnotesdocs('&lt;?php echo site_url('contacts/notes/'.$contact-&gt;id); ?&gt;','#notes-wrap'); 
                  }
              }
          }); 
      }
  </code></pre>
  <p>Controller Class:</p>
  <pre><code>function edit($id,$return=0)
      {
          if ($this-&gt;form_validation-&gt;run() == FALSE)
          {
              $edit['notes']= $this-&gt;contacts_model-&gt;load_notes($id); 
              $edit['leadid']= $id; 
          } else {
              $maindata=array('agents_id'=&gt;$this-&gt;input-&gt;post('agents_id'));
              $updated=$this-&gt;contacts_model-&gt;update($maindata,$id); 
              if($updated){
                  if($this-&gt;input-&gt;post('notes')){ 
                      foreach($this-&gt;input-&gt;post('notes') as $key =&gt; $note):  
                          if($note !='')  {
                              $smdata = array('contacts_id'=&gt;$id,'note'=&gt;$note,'added_by'=&gt;$this-&gt;session-&gt;userdata('clientsessuserid'));   
                              $this-&gt;contacts_model-&gt;save_notes($smdata); }
                      endforeach;
                  }
       }
  
      public function updatenote($id){
          if($this-&gt;form_validation-&gt;run() == TRUE)
          {
              $maindata = array('note'=&gt;$this-&gt;security-&gt;xss_clean($this-&gt;input-&gt;post('leadnote')));   
              $insid= $this-&gt;contacts_model-&gt;update_notes($maindata,array('notes_id'=&gt;$id)); 
              $c= $this-&gt;contacts_model-&gt;load($id,&quot;refno&quot;);
              $this-&gt;contacts_model-&gt;saveactivity(array('object_ref'=&gt;$c-&gt;refno,'object_id'=&gt;$insid,'activity'=&gt;'Note Updated','action'=&gt;'Updated'));  
              echo $insid;exit; 
          } 
      }
  
     public function savenotes($id){
      if($this-&gt;form_validation-&gt;run() == TRUE)
      {
          $maindata = array('contacts_id'=&gt;$id,'note'=&gt;$this-&gt;input-&gt;post('note'),'added_by'=&gt;$this-&gt;session-&gt;userdata('clientsessuserid'));   
          $insid= $this-&gt;contacts_model-&gt;save_notes($maindata); 
          $contacts= $this-&gt;contacts_model-&gt;load($id,&quot;refno&quot;);
          $this-&gt;contacts_model-&gt;saveactivity(array('object_ref'=&gt;$contacts-&gt;refno,'object_id'=&gt;$insid,'activity'=&gt;'Note Added','action'=&gt;'Added'));   
          echo $insid;exit; 
       } 
  }
  </code></pre>
  <p>Model Class:</p>
  <pre><code> function save_notes($maindata){
      $this-&gt;db-&gt;insert(&quot;crm_contacts_notes&quot;,$maindata); 
      return $this-&gt;db-&gt;insert_id(); 
  }
  function load($id)
  {
      $id=$this-&gt;db-&gt;escape_str($id);
      $cond=array('id'=&gt;$id);
      $this-&gt;db-&gt;where($cond);
      $this-&gt;db-&gt;from(&quot;crm_contacts&quot;);
      $query = $this-&gt;db-&gt;get();
      return $query-&gt;row();
  }
   function update_notes($maindata,$cond){
      return $this-&gt;db-&gt;update(&quot;crm_contacts_notes&quot;,$maindata,$cond);  
  } 
  </code></pre>
  <p>But doing so just refreshes the page whenever I click on the save button and nothing is sent to my database. I cannot even see if any requests are being made since whenever I click the save button, the page refreshes.</p>