0

I am new in php.I made and html Dynamic Table on which there are 2 following fields that are dynamically generated for each time the user presses Add/Remove Button:

Image Sample given below:

enter image description here

Here the html Code given below:

<!DOCTYPE html>
<html>
   <head>
      <style>
         table, th, td {
         border-collapse: collapse;
         }
      </style>
      <script>
         function addMore() {
             var table = document.getElementById("myTable");
             var row = table.insertRow(-1);
             var cell1 = row.insertCell(-1);
             var cell2 = row.insertCell(-1);
         
         var x = document.getElementById("myTable").rows[1].cells;
            
             cell1.innerHTML =  x[0].innerHTML;
             cell2.innerHTML = x[1].innerHTML;
         }
         
         
         function removeLast() {
             document.getElementById("myTable").deleteRow(-1);
         }
         
      </script>
   </head>
   <body>
      <form  action="data.php" method="post">
         <table id="myTable">
            <tr>
               <th>Options</th>
               <th>User Input</th>
            </tr>
            <tr>
               <td>
                  <select class="mySelect" name="DESCR" >
                     <option  disabled="" selected="">Select</option>
                     <option  value="1">A</option>
                     <option  value="2">B</option>
                     <option  value="3">C</option>
                     
                     
                  </select>
               </td>
               <td> <input type="text" name="ALAMT"></td>
            </tr>
         </table>
         
      </form>
      <br>
      <button onclick="addMore()">Add New</button>
      <button onclick="removeLast()">Remove</button>
      <button onclick="myFunction()">Try it</button>
   </body>
  
</html>

After choosing option and inputing vaules to this table,i want to submit their data to php using $_POST[ ] Because they are Dynamic,it will be time consuming & inefficient to maintain unique name or Id for each of them.So,What code should i write in php to do so.please let me know for any further information.Thanks

5
  • 1
    I can suggest you to build a JSON model of your dynamic table and post it to your php script Commented Dec 28, 2015 at 13:30
  • Thanks @Inurosen for your info.is there any benefit doing it in JSON ? I don't know JSON, can you give some easy useful example to do so.Thanks Commented Dec 28, 2015 at 13:36
  • 1
    The benefit is simple. JSON basically is a javascript object and it's easiest way to build it. Commented Dec 28, 2015 at 13:41
  • 1
    Does this answer help you in any way? Also, this question Commented Dec 28, 2015 at 13:41
  • I did this echo $_POST["myName"];.But did not work.any clue.Thanks @Inurosen Commented Dec 28, 2015 at 13:54

1 Answer 1

1

You Have to Take array as name of the new added field everytime. and that find the array count and make loop and get the value and put that value in the insert query.

used this script to add whole the textboxes

<script type="text/javascript">
        $(document).ready(function () {
            $('.add_more').live('click', function () {
                $(this).before('<div class="one del"> <div class="two"><label class="label" for="name">Title:</label><input type="text" class="validate[required]" value="" name="caption[]" /></div><div class="two"><label class="label" for="name">Value:</label><input type="text" class="validate[required]" value="" name="values[]"/></div><i class="fa fa-trash-o add_del"></i></div>');
            });
            $('.add_del').live('click', function () {
                $(this).parent().remove()
            });
        });

    </script>

Then

     for ($i = 0; $i < count($_POST['caption']); $i++) {
        $caption = $_POST['caption'][$i];
        $sp_values = $_POST['values'][$i];

    $sql1 = "INSERT INTO `sub_project` (`parent`, `sp_title`, `sp_values`) VALUES ('$insert', '$caption', '$sp_values')";
        $insert1 = $obj_db->sqlquery($sql1);

}

Hope this help you

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

Comments

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.