How to submit/save to DB the cell's input data in dynamically added rows on a table using javascript /php

382

I can't get the input data inside the text boxes which are present inside the newly added dynamic rows in the table. Please note that the dynamic row adding is not only on the last row of the table but just below row where the the add button is present.

  `while($rows = mysql_fetch_array($result))
    {

    echo '<td>'.$rows["id"].'</td>
    <td id"].'">'.$rows["item"].'</td>
    <td id"].'">'.$rows["cost"].'</td>

   <td>
   <div id="addinput">
    <p>
  <button name="add" onclick="myFunction(this)" value="'.$btn_count.'">        <fontcolor="#FF0000" size="4">+</font></button>
</p>
</div>
</td>


<script>
function myFunction(y)
 {

  var f = y.parentNode;
  var d = f.parentNode;
  var g = d.parentNode;
  var c =g.parentNode.rowIndex;  
  var table = document.getElementById("myTable");
  var row = table.insertRow(c+1);


  var cell1 = row.insertCell(0);
  var cell2 = row.insertCell(1);

  var cell7 = row.insertCell(6);
  cell1.innerHTML = "<form name=frm_add action='index.php' method='POST' ><input  type=text name=txt_id size=10 placeholder=Id>";
  cell2.innerHTML = "<input type=text name=txt_item id=item_name size=10  placeholder=Item >";
cell3.innerHTML = "<input type=text name=txt_cost size=10 placeholder=0.00>";
cell4.innerHTML = "<input type=text name=txt_wp size=10 placeholder=0.00>";
cell5.innerHTML = "<input type=text name=txt_rp size=10 placeholder=0.00>";

cell6.innerHTML = "<input type='submit' name='submit_adding' value='Save11' size=10 >
</form>";

document.getElementById("myTable");
}

after this iwrote the below code to submit the input text boxes data into DB

<?php
   if(isset($_POST['submit_adding']))
    {
     $item=$_POST['txt_item'];
     $cost=$_POST['txt_cost'];
     $wp=$_POST['txt_wp'];
     $rp=$_POST['txt_rp'];

     $q= $_POST['q'];

      if($p > 0)
       {
         $sql = "insert into shop".$q."_products (item,cost,wp,rp)values('$item','$cost','$wp','$rp')";
       }
         else
          {
           $sql = "insert into shop".$q."_products (item,cost,wp,rp)values('$item','$cost','$wp','$rp')";
          }
  mysql_query($sql); 
  ?>


<script>
alert("Successfully Added !!");
window.location.href="index.php?p=<?php echo $q;?>";
window.location.href="index.php";

</script>`

but its not working..

People are also looking for solutions to the problem: php - yii2 framework message language

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.