5

I have created a dynamic table using php.Now I want to add this functionality.enter image description here

When we click the submit button, It should automatically add data to the database and create new row and previous row should be disabled that it can't be edited.How to do this ?

<?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 Answers 2

4

Try simply disable the all previous row input with $('tr').find('input').prop('disabled',true) before new row append .

Jquery Documentation of 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>

Sign up to request clarification or add additional context in comments.

4 Comments

I tried both of answers but none of them work.my j query version 3.2.1 and browser is chrome.
@Manusha see my updated answer.jquery v 3.2.1 is not problem
I appreciate your help.But stiil the same.No change.I cant understand why.
I found the problem. $('tr').find('input').prop('disabled',true); semi colon is missing in this statement. Thank you very much for your help.
0

Add $('#addr'+(i-1)).find('input').attr('disabled',true); to your logic

Demo : 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++; 
  });
});

1 Comment

how do you submit this to the database? I tried: $.post("add_test2.php", { uid : uid+i },function(response){ console.log(response); but it never gets called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.