使用按钮点击动态添加新行到表格中的PHP实现

5
我已经使用php创建了一个动态表格。现在我想要添加这个功能。enter image description here 当我们点击提交按钮时,它应该自动将数据添加到数据库中,并创建新的行,以前的行应该被禁用,不能编辑。如何实现这一点?
<?php 
session_start();
require_once "header.php";
?>
<body>
<div class="text-center">
<h1>PAYMENT PAGE</h1>
</div>
<hr>

  <div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-bordered table-hover" id="tab_logic">
                <thead>
                    <tr >
                        <th class="text-center">
                            #
                        </th>
                        <th class="text-center">
                            User ID
                        </th>
                        <th class="text-center">
                            Name
                        </th>
                        <th class="text-center">
                            NIC
                        </th>
                        <th class="text-center">
                            Amount
                        </th>
                        <th class="text-center">
                            Date
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <tr id='addr0'>
                        <td>
                        1
                        </td>
                        <td>
                        <input type="text" name='uid'  placeholder='User ID' class="form-control"/>
                        </td>
                        <td>
                        <input type="text" name='uname' placeholder='Name' class="form-control"/>
                        </td>
                        <td>
                        <input type="text" name='nic' placeholder='NIC' class="form-control"/>
                        </td>
                        <td>
                        <input type="text" name='amount' placeholder='Amount' class="form-control"/>
                        </td>
                        <td>
                        <input type="date" name='dt' placeholder='Date' class="form-control"/>
                        </td>
                    </tr>
                    <tr id='addr1'></tr>
                </tbody>
            </table>
        </div>
    </div>
    <button id="add_row" class="btn btn-primary btn-lg pull-left" >SUBMIT</button>
</div>


</body>
</html> 


     $(document).ready(function(){
      var i=1;
     $("#add_row").click(function(){
      $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input type='text' name='uid"+i+"'  placeholder='User ID' class='form-control input-md'/></td><td><input type='text' name='uname"+i+"' placeholder='Name' class='form-control input-md'/></td><td><input type='text' name='nic"+i+"' placeholder='NIC' class='form-control input-md'/></td><td><input type='text' name='amount"+i+"' placeholder='Amount' class='form-control input-md'/></td><td><input type='date' name='dt"+i+"' placeholder='Date' class='form-control input-md'/></td>");

      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      i++; 
  });
});
2个回答

4

在添加新行之前,尝试简单地使用 $('tr').find('input').prop('disabled',true) 禁用所有先前的 row 输入。

Jquery prop() 文档

Updated jquery version 3.2.1

$(document).ready(function() {
  var i = 1;
  $("#add_row").click(function() {
  $('tr').find('input').prop('disabled',true)
    $('#addr' + i).html("<td>" + (i + 1) + "</td><td><input type='text' name='uid" + i + "'  placeholder='User ID' class='form-control input-md'/></td><td><input type='text' name='uname" + i + "' placeholder='Name' class='form-control input-md'/></td><td><input type='text' name='nic" + i + "' placeholder='NIC' class='form-control input-md'/></td><td><input type='text' name='amount" + i + "' placeholder='Amount' class='form-control input-md'/></td><td><input type='date' name='dt" + i + "' placeholder='Date' class='form-control input-md'/></td>");

    $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
    i++;
  });
});
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>

<div class="text-center">
  <h1>PAYMENT PAGE</h1>
</div>
<hr>

<div class="container">
  <div class="row clearfix">
    <div class="col-md-12 column">
      <table class="table table-bordered table-hover" id="tab_logic">
        <thead>
          <tr>
            <th class="text-center">
              #
            </th>
            <th class="text-center">
              User ID
            </th>
            <th class="text-center">
              Name
            </th>
            <th class="text-center">
              NIC
            </th>
            <th class="text-center">
              Amount
            </th>
            <th class="text-center">
              Date
            </th>
          </tr>
        </thead>
        <tbody>
          <tr id='addr0'>
            <td>
              1
            </td>
            <td>
              <input type="text" name='uid' placeholder='User ID' class="form-control" />
            </td>
            <td>
              <input type="text" name='uname' placeholder='Name' class="form-control" />
            </td>
            <td>
              <input type="text" name='nic' placeholder='NIC' class="form-control" />
            </td>
            <td>
              <input type="text" name='amount' placeholder='Amount' class="form-control" />
            </td>
            <td>
              <input type="date" name='dt' placeholder='Date' class="form-control" />
            </td>
          </tr>
          <tr id='addr1'></tr>
        </tbody>
      </table>
    </div>
  </div>
  <button id="add_row" class="btn btn-primary btn-lg pull-left">SUBMIT</button>
</div>


我尝试了这两个答案,但都没有起作用。我的 jQuery 版本是 3.2.1,浏览器是 Chrome。 - Manusha
@Manusha,请查看我的更新答案。jquery v 3.2.1不是问题。 - prasanth
我感谢你的帮助,但是还是一样。没有改变。我不明白为什么。 - Manusha
2
我找到了问题。 $('tr').find('input').prop('disabled',true); 这个语句缺少分号。非常感谢您的帮助。 - Manusha

0
$('#addr'+(i-1)).find('input').attr('disabled',true); 添加到您的逻辑中。 演示https://jsfiddle.net/kw4koyvk/
$(document).ready(function(){
      var i=1;
     $("#add_row").click(function(){
      $('#addr'+(i-1)).find('input').attr('disabled',true);
      $('#addr'+i).html("<td>"+ (i+1) +"</td><td><input type='text' name='uid"+i+"'  placeholder='User ID' class='form-control input-md'/></td><td><input type='text' name='uname"+i+"' placeholder='Name' class='form-control input-md'/></td><td><input type='text' name='nic"+i+"' placeholder='NIC' class='form-control input-md'/></td><td><input type='text' name='amount"+i+"' placeholder='Amount' class='form-control input-md'/></td><td><input type='date' name='dt"+i+"' placeholder='Date' class='form-control input-md'/></td>");

      $('#tab_logic').append('<tr id="addr'+(i+1)+'"></tr>');
      i++; 
  });
});

你怎么将这个提交到数据库?我尝试了:$.post("add_test2.php", { uid : uid+i },function(response){ console.log(response);但它从未被调用。 - sspence65

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接