javascript - 在 php 和 html5 中使用 javascript 添加行

标签 javascript php

参见图片。我想在单击“添加”按钮时重复相同的字段。我是 javascript 和 php 的新手。我尝试使用 javascript 添加行的代码,但它不起作用。谁能帮我写一下代码。

 <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>

/image/VlKqy.jpg

最佳答案

使用此代码在“添加事件”的 onclick 事件中为“名称”添加一个新行。每次单击“添加事件”时,都会为“名称”创建一个新行。

<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>

关于javascript - 在 php 和 html5 中使用 javascript 添加行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35376465/

相关文章:

javascript - d3js : why doesn't first bar disappear using exit method?

javascript - 将 `c > 3 && r && Object.defineProperty(target, key, r), r`重写为 "normal"演示

javascript - 调用chrome.tabs.query后,结果不可用

php - 在for循环中使用MySQL从数据库中获取数据

php - 是否可以在不创建 .ssh 目录的情况下执行 ssh 命令?

javascript - 定义 css 样式的 div 元素

javascript - React 生命周期循环

PHP:从文件中的某个点读取

php - 如何从MySQL PHP数据库中回显一个未选择的值

php - session 变量在 MySQL 语句中不起作用