如何在codeigniter中使用ajax在mysql数据库中插入数据?

Posted

技术标签:

【中文标题】如何在codeigniter中使用ajax在mysql数据库中插入数据?【英文标题】:How To insert data in mysql database using ajax in codeigniter? 【发布时间】:2015-09-14 11:53:54 【问题描述】:

我正在尝试使用 ajax 验证我的表单并在 mysql 数据库中插入数据。提交验证既未发生,也未插入数据。我在 codeigniter 框架中进行此操作。我是 ajax 的新手。我无法弄清楚哪里出错了。这是我的代码

查看:

      <script  type="text/javascript">
      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 && 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:'<?php echo base_url(); ?>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;
  );
</script>


        <form id="registration-form">
          <div class="register">
                    <div class="row">
                          <div class="col1">
                              <label for="first_name">First Name<span>*</span></label> <br/>
                              <input type="text" name="first_name"/>
                    <li class="first_name error"></li>
                          </div>

                          <div class="col2">
                                Last Name<br/>
                                <input type="text" name="last_name"/>
                          </div>
                    </div>

                    <div class="row">
                          <div  class="col1">
                                Date Of Birth <br/>
                                <input type="text" name="dob"/>
                          </div>

                            <div  class="col2">
                              Gender
                              <br/>
                                <input type="radio" name="gender" value="Male" /> Male
                                <input type="radio" name="gender" value="Female" /> Female
                            </div>
                    </div>


                    <div class="row">
                          <div class="col1">
                                Email<br/>
                                <input type="text" name="email_id"/>
                    <li class="email-id error"></li>
                          </div>
                          <div class="col2">
                                Password<br/>
                                <input type="password" name="password"/>
                          </div>
                    </div>

                    <div class="row">
                            <div class="col">
                                  Address<br/>
                                  <textarea name="address" rows="2" ></textarea>
                            </div>
                    </div>

                    <div class="row">
                          <div class="col1">
                                Zipcode<br/>
                                <input type="text" name="zipcode"/>
                          </div>
                           <div class="col2">
                                  Phone<br/>
                                  <input type="text" name="phone"/>
                            </div>
                    </div>

                    <div class="row">
                        <div class="col3">
                          <input class="" type="button" id="submit_enquiry" name="submit_enquiry" value="Submit Enquiry" />
                        </div>
                    </div> 
          </div>
        </form>

控制器:

public function register_user()
    
        $register_user  = $this->member_model->add_user();
        if($register_user)
                        
            return true;
          
        else 
          
            return false;
        
      

型号:

public function add_user()

    $add_user = array(
                    'mem_name'=> $this->input->post('first_name'),
                    'mem_lastname'=> $this->input->post('last_name'),
                    'mem_dob'=> $this->input->post('dob'),
                    'mem_gender'=> $this->input->post('gender'),
                    'mem_email'=> $this->input->post('email_id'),
                    'mem_address'=> $this->input->post('address'),
                    'mem_zipcode'=> $this->input->post('zipcode'),
                    'mem_phone'=> $this->input->post('phone'),
                    'mem_password'=> $this->input->post('password'),

    );

    $insert = $this->db->insert('membership', $add_user);
    $insert_id = $this->db->insert_id();
    return  $insert_id;

请帮帮我....

【问题讨论】:

提交表单时,我会在控制器端使用 form_validation, 什么确切不起作用? AJAX 请求本身?在 PHP 中处理它?将数据写入数据库? 【参考方案1】:

在你的ajax中改变这个添加撇号(')

data: 
                  'first_name' : first_name ,'last_name' : last_name ,'dob' : dob , etc...
               ,

将此添加到您的控制器成员中

    function register()
       $add_user = array(
                    'mem_name'=> $this->input->post('first_name'),
                    'mem_lastname'=> $this->input->post('last_name'),
                    'mem_dob'=> $this->input->post('dob'),
                    'mem_gender'=> $this->input->post('gender'),
                    'mem_email'=> $this->input->post('email_id'),
                    'mem_address'=> $this->input->post('address'),
                    'mem_zipcode'=> $this->input->post('zipcode'),
                    'mem_phone'=> $this->input->post('phone'),
                    'mem_password'=> $this->input->post('password'),
      );
      $this->member_model->add_user($add_user);
    

在您的 member_model 中

function add_user($add_user)
    $insert = $this->db->insert('membership', $add_user);

【讨论】:

我尝试了您的解决方案,但它不起作用.. db @ Arif 中的表单验证和数据都没有输入 什么表单验证?我这里没有看到那个代码,只是js表单验证【参考方案2】:

您可以在提交处理程序上使用 jquery validate 和 ajay。请尝试以下代码:

$(function () 
    $("#FormId").validate(
        errorElement: 'span', errorClass: 'help-block',
        rules: 
            Tag: 
                required: true
            ,
           Table: 
                required: true
            ,
            Field: 
                required: true
            ,
        ,
        messages: 
            Tag: 
                required: "Tag is required."
            ,
            Table: 
                required: "Table is required."
            ,
            Field: 
                required: "Field is required."
            ,
        ,
        highlight: function (element) 
            $(element).closest('.form-group').addClass('has-error');
        ,
        unhighlight: function (element) 
            $(element).closest('.form-group').removeClass('has-error');
        ,
        success: function (element) 
            $(element).closest('.form-group').removeClass('has-error');
            $(element).closest('.form-group').children('span.help-block').remove();
        ,
        errorPlacement: function (error, element) 
            error.appendTo(element.closest('.form-group'));
        ,
        submitHandler: function (form) 
            $("#saveButton").button('loading');
            $.post('', $("#FormId").serialize(), function (data) 
                //Write your code here
            );
        
    );
);```

【讨论】:

以上是关于如何在codeigniter中使用ajax在mysql数据库中插入数据?的主要内容,如果未能解决你的问题,请参考以下文章

在codeigniter中如何使用ajax登录后重定向

Codeigniter 如何在控制器中接收 ajax post 数据

当数据在javascript codeigniter php中使用ajax成功功能时,如何禁用页面重新加载?

如何创建ajax请求以使用DataTable和Codeigniter并在同一文件中返回

如何使用 CodeIgniter 中的 AJAX 函数更改网页的数据

在 CodeIgniter 中使用 AJAX 从数据库中获取单行?