0

see the picture .I want to repeat the same field when i click on the add button. I am new to javascript and php. I tried the code for adding row using javascript, but its not working. Can any one help me to write the code.

 <form action="act_master.php" method="post">
        <fieldset>
            <legend>Activity</legend>
            <table id="tb1">
                <tr>
                    <td>Name</td>
                    <td><input type="text" name="activityname" required></td>
                </tr>
                <tr>
                    <td>Month</td>
                    <td><select name="month" required>
                        <option></option>
                            <?php
                                $conn=new mysqli("localhost","root","","project");
                                $menu=" "; 
                                $sql="SELECT name FROM month"; //selection query
                                $rs = mysqli_query($conn, $sql);//odbc_exec($conn,$sql);s
                                if(mysqli_num_rows($rs) > 0) {
                                    // output data of each row
                                     while($row = mysqli_fetch_assoc($rs)) {
                                        $menu .= "<option value=".$row['name'].">" . $row['name']. "</option>";
                                        }
                                }
                                echo $menu;
                                mysqli_close($conn); 
                            ?>        
                        </select></td>
                </tr>
                <tr>
                    <td>Date</td>
                    <td><select name="date" required>
                        <option></option>
                        <?php
                             for ($i=1; $i<=31; $i++)
                             {
                        ?>
                            <option value="<?php echo $i;?>"><?php echo $i;?></option>
                        <?php
                            }
                        ?></td>
                </tr>
               </table>
           <input type="submit" value="Add Activity" onclick="addrow(tb1)">


        </fieldset>
        <script>
            function addrow(tb1){
            var table = document.getElementById(tb1);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "text";
            element1.name="activityname";
            cell1.appendChild(element1);
        } 
        </script>
    </form>
    </div>

https://i.sstatic.net/VlKqy.jpg

4
  • shear your code might be er can help you out ... Commented Feb 13, 2016 at 5:30
  • we can't see your code . Commented Feb 13, 2016 at 5:33
  • Picture or some code please ??? Commented Feb 13, 2016 at 5:33
  • i am new to php and javascript.. code is added.. can anyone help me to write the code Commented Feb 13, 2016 at 6:10

1 Answer 1

2

Use this code to add a new row for Name at onclick event of Add Activity .It is create a new row for Name each time when you click Add activity.

<form action="act_master.php" method="post">
  <fieldset>
    <legend>
      Activity
    </legend>
    <table id="tb1">
      <tr>
        <td>
          Name
        </td>
        <td>
          <input type="text" name="activityname" required>
        </td>
      </tr>
      <tr>
        <td>
          Month
        </td>
        <td>
          <select name="month" required>
            <option>
            </option>
            <?php
$conn=new mysqli("localhost","root","","project");
$menu=" "; 
$sql="SELECT name FROM month"; //selection query
$rs = mysqli_query($conn, $sql);//odbc_exec($conn,$sql);s
if(mysqli_num_rows($rs) >
0) {
// output data of each row
while($row = mysqli_fetch_assoc($rs)) {
$menu .= "
<option value=".$row['name'].">
" . $row['name']. "
</option>
";
}
}
echo $menu;
mysqli_close($conn); 
?>

                      </select>
                  </td>
              </tr>
              <tr>
                <td>
                  Date
                </td>
                <td>
                  <select name="date" required>
                    <option>
                    </option>
                    <?php
for ($i=1; $i
<=31; $i++)
{
?>
  <option value="
<?php echo $i;?>
">
  <?php echo $i;?>
  </option>
  <?php
}
?>
                      </td>
                  </tr>
          </table>
          <input type="submit" value="Add Activity" onclick="addrow()">


  </fieldset>
  <script>
    function addrow(){

      var table = document.getElementById("tb1");
      var row = table.insertRow(0);
      var cell1 = row.insertCell(0);
      var cell2 = row.insertCell(1);
      cell1.innerHTML = "Name";
      cell2.innerHTML = "<input type='text' name='activityname' required>";


    }

  </script>
</form>
</div>
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.