title: How To insert data in mysql database using ajax in codeigniter?
body: <p>I am trying to validate my form and insert the data in mysql database using ajax.Neither on submit validation is happening nor data is being inserted.I am making this in codeigniter framework. I am new bie to ajax.I am not able to figure out where am going wrong .Here is my code </p>
  
  <p>View :</p>
  
  <pre><code>      &lt;script  type="text/javascript"&gt;
        function validate_name(first_name){
          if(first_name.trim() == '' || first_name.length == 0){
          $('.first_name').show();
           $('.first_name').text('Please enter your name');
           return false;
      } else {
        $('.first_name').hide();
        return true;
      }
    }
  
  
    function validate_email(email_id){
      var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
  
      if(email_id.trim() == '' || email_id.length == 0){
        $('.email-id').show();
        $('.email-id').text('Please enter email address');
        return false;
      }else if(!pattern.test(email_id)) {
        $('.email-id').show();
        $('.email-id').text('Please enter valid email address');
        return false;
      } else {
        $('.email-id').hide();
        return true;
      }
    }
  
    function validate_inquiry_form(first_name,email_id){
      var username_validate = validate_name(first_name);
      var email_validate = validate_email(email_id);
  
      if(username_validate == true &amp;&amp; email_validate == true){
        return true;
      } else {
        return false;
      }
    }
  
    $('#submit_enquiry').click(function(){
  
        var first_name      = $("input[name=first_name]").val();
        var last_name       = $("input[name=last_name]").val();
        var dob             = $("input[name=dob]").val(); 
        var gender          = $("input[name=gender] :radio:checked").val();
        var email_id        = $("input[name=email_id]").val();
        var password        = $("input[name=password]").val();
        var address         = $("input[name=address]").val();
        var phone           = $("input[name=phone]").val();
        var zipcode         = $("input[name=zipcode]").val();
  
        var validate_form = validate_inquiry_form(first_name,email_id);
        if(validate_form == true){
          $.ajax({
            url:'&lt;?php echo base_url(); ?&gt;member/register',
            type:'POST',
            data:{ 
                    first_name : first_name ,last_name : last_name ,dob : dob ,male : male ,female : female , email_id : email_id ,password : password ,phone : phone , address : address , zipcode : zipcode
                 },
  
            success: function(data) {
  
              console.log(data);
            }
          });
        } else {
          return false;
        }
        return false;
    });
  &lt;/script&gt;
  
  
          &lt;form id="registration-form"&gt;
            &lt;div class="register"&gt;
                      &lt;div class="row"&gt;
                            &lt;div class="col1"&gt;
                                &lt;label for="first_name"&gt;First Name&lt;span&gt;*&lt;/span&gt;&lt;/label&gt; &lt;br/&gt;
                                &lt;input type="text" name="first_name"/&gt;
                      &lt;li class="first_name error"&gt;&lt;/li&gt;
                            &lt;/div&gt;
  
                            &lt;div class="col2"&gt;
                                  Last Name&lt;br/&gt;
                                  &lt;input type="text" name="last_name"/&gt;
                            &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="row"&gt;
                            &lt;div  class="col1"&gt;
                                  Date Of Birth &lt;br/&gt;
                                  &lt;input type="text" name="dob"/&gt;
                            &lt;/div&gt;
  
                              &lt;div  class="col2"&gt;
                                Gender
                                &lt;br/&gt;
                                  &lt;input type="radio" name="gender" value="Male" /&gt; Male
                                  &lt;input type="radio" name="gender" value="Female" /&gt; Female
                              &lt;/div&gt;
                      &lt;/div&gt;
  
  
                      &lt;div class="row"&gt;
                            &lt;div class="col1"&gt;
                                  Email&lt;br/&gt;
                                  &lt;input type="text" name="email_id"/&gt;
                      &lt;li class="email-id error"&gt;&lt;/li&gt;
                            &lt;/div&gt;
                            &lt;div class="col2"&gt;
                                  Password&lt;br/&gt;
                                  &lt;input type="password" name="password"/&gt;
                            &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="row"&gt;
                              &lt;div class="col"&gt;
                                    Address&lt;br/&gt;
                                    &lt;textarea name="address" rows="2" &gt;&lt;/textarea&gt;
                              &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="row"&gt;
                            &lt;div class="col1"&gt;
                                  Zipcode&lt;br/&gt;
                                  &lt;input type="text" name="zipcode"/&gt;
                            &lt;/div&gt;
                             &lt;div class="col2"&gt;
                                    Phone&lt;br/&gt;
                                    &lt;input type="text" name="phone"/&gt;
                              &lt;/div&gt;
                      &lt;/div&gt;
  
                      &lt;div class="row"&gt;
                          &lt;div class="col3"&gt;
                            &lt;input class="" type="button" id="submit_enquiry" name="submit_enquiry" value="Submit Enquiry" /&gt;
                          &lt;/div&gt;
                      &lt;/div&gt; 
            &lt;/div&gt;
          &lt;/form&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>public function register_user()
      {
          $register_user  = $this-&gt;member_model-&gt;add_user();
          if($register_user)
          {                
              return true;
          }  
          else 
          {  
              return false;
          }
      }  
  </code></pre>
  
  <p>Model :</p>
  
  <pre><code>public function add_user()
  {
      $add_user = array(
                      'mem_name'=&gt; $this-&gt;input-&gt;post('first_name'),
                      'mem_lastname'=&gt; $this-&gt;input-&gt;post('last_name'),
                      'mem_dob'=&gt; $this-&gt;input-&gt;post('dob'),
                      'mem_gender'=&gt; $this-&gt;input-&gt;post('gender'),
                      'mem_email'=&gt; $this-&gt;input-&gt;post('email_id'),
                      'mem_address'=&gt; $this-&gt;input-&gt;post('address'),
                      'mem_zipcode'=&gt; $this-&gt;input-&gt;post('zipcode'),
                      'mem_phone'=&gt; $this-&gt;input-&gt;post('phone'),
                      'mem_password'=&gt; $this-&gt;input-&gt;post('password'),
  
      );
  
      $insert = $this-&gt;db-&gt;insert('membership', $add_user);
      $insert_id = $this-&gt;db-&gt;insert_id();
      return  $insert_id;
  }
  </code></pre>
  
  <p>Please help me ....</p>