簡體   English   中英

在php和html5中使用javascript添加行

[英]adding row using javascript in php and html5

看到圖片。當我單擊添加按鈕時,我想重復相同的字段。 我是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>

http://i.stack.imgur.com/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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM