如何将动态输入字段提交到数据库? [英] How to submit the dynamical input field into the database?

查看:62
本文介绍了如何将动态输入字段提交到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动态显示对我有用的输入字段.

I am displaying the input field dynamically which is working for me.

问题是

我必须提交表格.我已经尝试了一些下面的代码,但是没有用.

I have to submit the form. I have tried some code as below but it's not working.

我正在使用Codeigniter.

I am using Codeigniter.

控制器代码

public function register(){
$save = array(
   'pp_fileStatus' => $this->input->post('pp_fileStatus');
   'reasonDate' => $this->input->post('reasonDate');
   'reasonAmt' => $this->input->post('reasonAmt');
 );
$afterxss=$this->security->xss_clean($save);
        if ($afterxss) 
        { 
          $this->db->insert('tbl_register',$afterxss);
            $response['error'] = "true";
            $response['msg']   = "Successfully";

      }else{
          $response['error'] = "false";
          $response['msg']   = "Sometning wrong! please check the internet connection and try again";
      } 
   echo json_encode($response);          
}

我正在动态添加字段并增加名称.请让我知道我必须在这里使用的名字

I am adding the field dynamically and incrementing the name. Please let me know what name I have to use here

$save = array(
       'pp_fileStatus' => $this->input->post('pp_fileStatus');
       'reasonDate' => $this->input->post('reasonDate');
       'reasonAmt' => $this->input->post('reasonAmt');
     );

下面是用于动态添加输入字段的代码.

Below is the code for adding the input field dynamically.

$(document).ready(function() {
  var maxField = 10; //Input fields increment limitation
  var x = 1; //Initial field counter is 1
  var count = 2;
  var numberIncr = 1; // used to increment the name for the inputs
  var addrm = '';

  //Once add button is clicked
  $(document).on('click', '#clicktoadd', function() {
    //Check maximum number of input fields
    if (x < maxField) {
      x++; //Increment field counter
      numberIncr++;
      $(".medication_info").append('<select name="pp_fileStatus' + numberIncr + '" class="form-control multipleselect pp_fileStatus dynamicVal"><option value="" disabled selected>Status</option><option value="1">Open</option><option value="2">Close</option><option value="3">Pending</option></select>');
    }
    count++;

  });
  $(document).on('change', '.pp_fileStatus', function(event) {

    if (($(this).val() == '1') || ($(this).val() == '3')) {
      $(".medication_info").append('<div class="addbankField input-wrapper padding0"><div class="form-group"> <input type="text" name="reasonDate' + numberIncr + '"  class="form-control datetimepicker dynamicVal" placeholder="Date"></div></div><div class="addbankField input-wrapper"><div class="form-group"> <input type="text" name="reasonAmt' + numberIncr + '"  class="form-control commnumber dynamicVal" placeholder="amt"></div></div><input type="hidden" name="remark' + numberIncr + '"  class="form-control" placeholder="Remark">');
    } else {
      $(".medication_info").append('<div class="addbankField input-wrapper lbpflex padding0"><div class="form-group"> <input type="text" name="reasonDate' + numberIncr + '"  class="form-control datetimepicker dynamicVal" placeholder="Date"></div></div><div class="addbankField input-wrapper"><div class="form-group"> <input type="text" name="remark' + numberIncr + '"  class="form-control dynamicVal" placeholder="Remark"></div></div><input type="hidden" name="reasonAmt' + numberIncr + '" class="form-control" placeholder="amt">');
    }
  });

});



$('#register').on('submit', function(event) {
  event.preventDefault();
  // adding rules for inputs with class 'comment'
  $('.dynamicVal').each(function() {
    $(this).rules("add", {
      required: true
    })
  });
  // test if form is valid 
  if ($('#register').validate().form()) {
    $.ajax({
      //url:"process.php",
      url: baseUrl + "/Customer_control/register",
      type: "POST",
      dataType: "json",
      data: $('#register').serialize(),

      success: function(data) {
        alert("success");
      },
    }); // AJAX Get Jquery statment
  }
  //alert('hellow');
});
$('#register').validate({
  errorPlacement: function(error, element) {
    if (element.is("select")) {
      error.insertAfter(element.parent());
    } else {
      error.insertAfter(element);
    }

  }
});

<div id="clicktoadd"><a href="javascript:void(0);" class="btn btn-bg">Add More</a></div>
<form action="#" method="post" id="register" name="register">
  <div class="row">
    <div class="medication_info">
    </div>
  </div>

  <input type="submit" name="send" value="submit">
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.min.js"></script>

有人可以在这个问题上帮助我吗?

Can anyone here to help me out with this issue?

推荐答案

您可以将数组用于HTML形式的多个名称,然后使用PHP(CodeIgniter)中的Foreach循环获取值.

You can use arrays for multiple names in HTML form and then get the values using Foreach Loop in PHP (CodeIgniter).

这是更改代码的方法:更改此行:

Here is how you should change your code: Change your this line:

$(".medication_info").append('<select name="pp_fileStatus' + numberIncr + '" class="form-control multipleselect pp_fileStatus dynamicVal"><option value="" disabled selected>Status</option><option value="1">Open</option><option value="2">Close</option><option value="3">Pending</option></select>')

收件人:

$(".medication_info").append('<select name="pp_fileStatus[]" class="form-control multipleselect pp_fileStatus dynamicVal"><option value="" disabled selected>Status</option><option value="1">Open</option><option value="2">Close</option><option value="3">Pending</option></select>')

注意:我只是将选择字段名称更改为"pp_fileStatus []" ,然后删除numberIncr变量

Note: I just changed select field name to "pp_fileStatus[]" and remove numberIncr variable

现在,您可以像这样在控制器中访问此字段名称值.

Now you can access this field name values in your controller like this.

$pp_fileStatus = $this->input->post('pp_fileStatus');

这里$ pp_fileStatus是一个数组,包含pp_fileStatus的所有值.

Here $pp_fileStatus is an array and contains all the values of pp_fileStatus.

您也可以对其他表单字段执行同样的操作.

因此,您无需为字段添加名称,只需将其递增一个变量即可.

So you get rid of giving names to fields by incrementing one to a variable.

希望这可以解决您的问题.

Hope this solves your problem.

您可以像这样更新注册功能:

You can update your register function like this:

public function register(){

    $insert_array = [];

    $pp_fileStatus = $this->input->post('pp_fileStatus');
    $reasonDate    = $this->input->post('reasonDate');
    $reasonAmt    = $this->input->post('reasonAmt');
    $remark    = $this->input->post('remark');

    foreach ($pp_fileStatus as $key => $value) {

        $insert_array[] = array(
                                   'pp_fileStatus' => $value,
                                   'reasonDate' => $reasonDate[$key],
                                   'reasonAmt' => $reasonAmt[$key],
                                   'remark' => $remark[$key]
                                 );

    }

    $this->db->insert_batch('tbl_register',$insert_array);
}

根据您的需要更新此功能

这篇关于如何将动态输入字段提交到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆