Add default post value into dynamic created input row field in php

I have two small form. I want to enter one value (Name) into another form by adding that in 2nd form. But the problem which i am facing is that for single row, its work fine, but when i created dynamic more dynamic row, then this default value missing from that dynamic created rows.

Here is my complete php code : Form 1

          <form method="POST" action="f1.php">              
           <label>Catagory:</label> <input type="text" name="name" id="name" class="form-control">
             <label>Catagory:</label> <input type="text" name="lname" id="lname" class="form-control">  
           <label>Catagory:</label> <input type="text" name="age" id="age" class="form-control">                    
          <input type="submit" name="submit" id="submit" value="Submit">
             </form>

f1.php

 <?php
        // getting all values from the HTML form
           if(isset($_POST['submit']))
              {        
             $name = $_POST['name'];     
$lname = $_POST['lname'];
$age = $_POST['age'];   
}

           // using sql to create a data entry query
            $sql = "INSERT INTO aaa(name, lname, age)
            VALUES ('$name', '$lname', '$age')";   

            // send query to the database to add values and confirm if successful
                  $rs = mysqli_query($con, $sql);
                     if($rs)
                   {
    echo "Entries added!";
}

               // close connection
                     mysqli_close($con);
                      ?>

                     <html><body>
                      <form>
                       <table class="table table-bordered">
                     <thead class="table-success" style="background-color: #3fbbc0;">
                             <tr>                            
                        <th width="15%"><center>Name</th>                          
                       <th width="5%"></th>  
                                               
                                 <button type="button" class="btn btn-sm btn-success" onclick="BtnAdd()">Add Item</button>                         
                               </th>
                        </tr>
                                  </thead>
                                 <tbody id="TBody">
                      <tr id="TRow" class="d-none">    
<td><input type="text" class="qty form-control text-end" name="name[]" id="name" value = "<?php echo $name; ?>"></td>       
<td class="NoPrint"><button type="button" class="btn btn-success"  style="line-height: 1;" onclick="BtnDel(this)">x</button></td>
</tr>
                    </tbody>
 
        
                  </table>

script to add remove new row dynamically

<script type="text/javascript">

function GetPrint()
{
    /*For Print*/
    window.print();
}

function BtnAdd()
{
    /*Add Button*/
    var v = $("#TRow").clone().appendTo("#TBody") ;
    $(v).find("input").val('');
    $(v).find("input").autocomplete({
        source: 'backend-script.php'  
    });
    $(v).removeClass("d-none");
    $(v).find("th").first().html($('#TBody tr').length - 1);
}

function BtnDel(v)
{
    /*Delete Button*/
       $(v).parent().parent().remove(); 
       GetTotal();

        $("#TBody").find("tr").each(
        function(index)
        {
           $(this).find("th").first().html(index);
        }

       );
}

</script>

Well yes, because you tell Javascript to erase the value.
$(v).find("input").val("");

Assuming you have exactly 1 default value you’re expecting in each, you may be trying to do this:

  let ip = $(v).find("input"); //Dont find more than once.
  ip.val(ip.prop("defaultValue"));

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.