如何从自动生成的字段中保存数据并将其与单个ID相关联?

时间:2017-05-22 13:26:56

标签: javascript ajax codeigniter

我想保存从自动生成的字段到数据库的项目的里程碑。我希望从与项目ID关联的字段中获取几个里程碑数据集。

autogenerated fields

我的表格如下:

<Form components -->
  <form class="form-horizontal" role="form" action="#">
    <!-- Basic inputs -->
    <div class="panel panel-default">
      <div class="panel-heading">
        <h6 class="panel-title"><i class="icon-bubble4"></i>Set Up Milestone</h6></div>
      <div class="panel-body">                    
        <label class="col-sm-2">Project ID: </label>
        <div class="col-sm-10">
          <div class="row">
            <div class="col-sm-10">

              <select name="select" class="form-control input-lg">
                <option value="opt1">Select Project Id</option>
                <?php foreach ($listed_projects as $row):?> 
                <option value="opt2"><?php echo $row->no;?> <?php echo $row->project_name;?></option>
                <?php endforeach ?>

              </select>

            </div>
            <div class="col-sm-4">
              <label style="color: #0B7C14">Select the ID Value of the project</label>
            </div>                            
          </div>
        </div>

      </div>

      <div id="content" >  
        <div  class="panel-body">                  
          <label class="col-sm-2">Milestone: </label>
          <div class="col-sm-10">
            <div class="row" ">
              <div class="col-sm-10">
                <div class="">
                  <input name="attribute" type="text" class="form-control">
                </div>
              </div>                            
              <div class="col-sm-4">
                <label style="color: #0B7C14">Add the milestone</label>
              </div>                      
            </div>
          </div>
        </div>
      </div>
      <div class="col-sm-8" style="text-align: right;padding-top: 5px">
        <a href="javascript:void(0);"><span data-icon="" id="addfields" onclick="add_fields();"> Add field</span></a>
      </div>
  </form>

我的javascript在#content中插入字段

<script type="text/javascript">
  function add_fields() {
    var d = document.getElementById("content");

    d.innerHTML += '<div  class="panel-body"><label class="col-sm-2">Milestone: </label><div class="col-sm-10"><div class="row" "><div class="col-sm-10"><div class=""><input type="text" class="form-control" id="attribute"></div></div><div class="col-sm-4"><label style="color: #0B7C14">Add the milestone attribute</label></div></div></div></div>';
  }
</script>

如何将其保存到数据库中?哪个有milestone_no,project_id和里程碑?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您需要首先传递数据:

 $(document).on('submit', '.form-horizontal', function(event) {

  event.preventDefault(); //prevents from submitting form

  $.ajax({
    url: "<YOUR URL RECEIVING THE FORM DATA>",
    type: "post",
    dataType: 'json',
    data: $(this).serialize(),
    success: function(d) {

    }
  });

});

在接收表单数据的页面上,如下所示:

    $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean');
    $this->form_validation->set_rules('email', 'Emaid ID', 'trim|required|valid_email');
    $this->form_validation->set_rules('subject', 'Subject', 'trim|required|xss_clean');
    $this->form_validation->set_rules('message', 'Message', 'trim|required|xss_clean');

    $this->form_validation->set_message('xss_clean', 'Invalid characters.');

    //run validation on form input
    if ($this->form_validation->run() == FALSE)
    {


        $response['status'] = 'error';
        $response['errors'] = validation_errors();
        echo json_encode($response);
    }
    else
    {
           //insert your data
    }