title: How to control posting form data on button click in CodeIgniter
body: <p>Previously I posted an answer myself to this problem, believing that I've found a solution. However, just this morning, I found out that I was wrong. It's only partially solved. So, I've decided to remove that answer and edit this post altogether. </p>
  
  <p>In my CodeIgniter application, I have the following view page - <code>meal_add.php</code>, and there is a form there. There's a submit button below that form. The form method is supposed to be <code>"post"</code>. If I click on the submit button, data is saved in database. The code is below:</p>
  
  <pre><code>&lt;form action="javascript:checkTime();" method="post"&gt;
              &lt;fieldset&gt;
                  &lt;legend id="add_employee_legend"&gt;Add Meal Information&lt;/legend&gt;
                  &lt;div&gt;
                      &lt;label id= "emp_id_add_label"&gt;Employee ID:&lt;/label&gt;
                      &lt;input type="text" name="emp_id" id = "employee_id_add" placeholder="Employee ID" required="1"/&gt;
                  &lt;/div&gt;
                  &lt;br&gt;
                  &lt;div&gt;
                      &lt;label id= "is_guest_add_label"&gt;Guests?&lt;/label&gt;
                      &lt;input type="checkbox" name ="is_guest_checkbox" class ="guestcheck" checked="checked" value="1" onchange="valueChanged()"/&gt;
                  &lt;/div&gt;
                  &lt;br&gt;
                  &lt;div id = "guestnum"&gt;
                      &lt;label id= "num_of_guest_add_label"&gt;No. of Guests:&lt;/label&gt;
                      &lt;input type="text" name="num_of_guest" id = "num_of_guest_add" placeholder='0'/&gt;
                  &lt;/div&gt;
                  &lt;div&gt;
                      &lt;label id= "remarks_add_label"&gt;Remarks:&lt;/label&gt;
                      &lt;textarea rows="1" cols="20" style="margin-left: 35px"&gt;&lt;/textarea&gt;
                  &lt;/div&gt;
                  &lt;input type="submit" name="submit" id = "meal_info_submit" value="Save Meal Information"/&gt;
                  &lt;button id = "cancel_button" onclick="location.href='&lt;?php echo base_url();?&gt;index.php/admin_logins/meal'"&gt;Cancel&lt;/button&gt;
              &lt;/fieldset&gt;    
          &lt;/form&gt;
  </code></pre>
  
  <p>Now, I want to do a certain condition checking on click of submit button. Based on the checking, I'll allow the submission of data and redirection of the page. So my JavaScript function goes as follows:</p>
  
  <pre><code>    &lt;script&gt;
      function checkTime()
      {
          var today = new Date();
          var hour = today.getHours();
          if(hour &lt; '12')
          {
              location.href='&lt;?php echo base_url();?&gt;index.php/meals/insert_meal_db';
          }
          else
          {
              alert("You can't place an order after 12 pm.");
              location.href='&lt;?php echo base_url();?&gt;index.php/admin_logins/meal';
          }
      }
      &lt;/script&gt;
  </code></pre>
  
  <p>This bit works fine - Whenever I try to submit data after 12 pm, an alert message says I can't place an order after 12 pm, halts my data submission and redirects to another page. However, when it's BEFORE 12 pm, data is supposed to be submitted on click, i.e. form method should be post. But it seems data is not submitted on click, the form method <code>"post"</code> is not working at all. All I get is just plain 0s in my database.</p>
  
  <p>My controller insert method - </p>
  
  <pre><code>public function insert_meal_db()
  {
      date_default_timezone_set('Asia/Dacca');
      $mdata['emp_id'] = $this-&gt;input-&gt;post('emp_id');
      $mdata['is_guest'] = $this-&gt;input-&gt;post('is_guest');
      $mdata['num_of_guest'] = $this-&gt;input-&gt;post('num_of_guest');
      $mdata['remarks'] = $this-&gt;input-&gt;post('remarks');
      if($mdata['num_of_guest'] == 0)
      {
          $mdata['bill'] = 35.00;
      }
  
      else
      {
          $mdata['bill'] = 35.00 + (35.00 * $mdata['num_of_guest']);
  
      }
      $res = $this-&gt;meal_model-&gt;insert_meal($mdata);
      if($res)
      {   
          $this-&gt;session-&gt;set_flashdata('message','Meal information added successfully');
          header('location:'.base_url()."index.php/meals/".$this-&gt;index());
  
      }
  }
  </code></pre>
  
  <p>Note that <code>$mdata['bill']</code> is not coming from <code>$this-&gt;input-&gt;post()</code>, it's coming from a calculation. So I get  data for $mdata['bill']` in my database based on the calculation, instead of a 0. But for other columns, I get plain 0s.</p>
  
  <p>My model insert method- </p>
  
  <pre><code>  //To add a new meal to the database
    public function insert_meal($data)
    {
        return $this-&gt;db-&gt;insert('meal', $data);
    }   
  </code></pre>
  
  <p>My table name is <code>meal</code>, database is MySQL and here is the table structure:</p>
  
  <pre><code>       Column Name                       Data Type
            id                               int PK 
          emp_id                      varchar(15) NOT NULL
         is_guest                        int NOT NULL
        num_of_guest                         int 
          bill                           int NOT NULL
         remarks                             text
  </code></pre>
  
  <p>The funny thing is - although my <code>emp_id</code> column is a <code>VARCHAR NOT NULL</code> column, the data that is being saved there is 0. </p>