【问题标题】:how to name objects dynamically created by javascript如何命名由javascript动态创建的对象
【发布时间】:2018-03-12 03:00:39
【问题描述】:

我想知道如何命名由以下代码创建的记录集。问题是每次单击“提交”时,只会将最近的一组发送到数据库。我想知道如何修改代码以便能够将所有数据发送到数据库中。谢谢。

function addRow(tableID) {
  var table = document.getElementById(tableID);
  var rowCount = table.rows.length;
  if (rowCount < 5) {
    var row = table.insertRow(rowCount);
    var colCount = table.rows[0].cells.length;
    for (var i = 0; i < colCount; i++) {
      var newcell = row.insertCell(i);
      newcell.innerHTML = table.rows[0].cells[i].innerHTML;
    }
  } else {
    alert("Maximum Number of Courses is 5");

  }
}

function deleteRow(tableID) {
  var table = document.getElementById(tableID);
  var rowCount = table.rows.length;
  for (var i = 0; i < rowCount; i++) {
    var row = table.rows[i];
    var chkbox = row.cells[0].childNodes[0];
    if (null != chkbox && true == chkbox.checked) {
      if (rowCount <= 1) {
        alert("Cannot Remove all the Courses.");
        break;
      }
      table.deleteRow(i);
      rowCount--;
      i--;
    }
  }
}
<form action="*.php" enctype="multipart/form-data" id="form" method="post" name="form">
  <table id="dataTable" class="form" border="1">
    <tbody>
      <p></p>
      <td>
        <input type="checkbox" name="chk[]" checked="checked" />
      </td>
      <td>
        <label for="course">Course</label>
        <select id="course" name="course">
          <option></option>
          <option>Course 1</option>
          <option>Course 2</option>
          <option>Course 3</option>
          <option>Course 4</option>
          <option>Course 5</option>
        </select>
      </td>

      <td>
        <label for="year">Year</label>
        <select id="year" name="year">
            <option>Select Year</option> 
            <option>2000</option>
            <option>2001</option>
            <option>2002</option>
        </select>
      </td>

      <td>
        <label for="semester">Semester</label>
        <select id="semester" name="semester">
            <option>Select Semester</option> 
            <option>1</option>
            <option>2</option>
            <option>3</option>
        </select>
      </td>
    </tbody>
  </table>
  <p></p>
  <input type="button" value="Add Course" onClick="addRow('dataTable')" />
  <input type="button" value="Remove Course" onClick="deleteRow('dataTable')" />
  <input id="button" name="submit" type="submit" value="Submit Data" />

  <p></p>

</form>

我们将非常感谢您的帮助

【问题讨论】:

    标签: javascript php jquery forms


    【解决方案1】:

    您需要为每个输入创建一个数组,而不是name="course" 写入name="course[]"(每个输入都相同)。例如:

    HTML:

    <form action="data.php" enctype="multipart/form-data" id="form" method="post" name="form">
      <table id="dataTable" class="form" border="1">
        <tbody>
          <p>
            <td >
              <input type="checkbox" name="chk[]" checked="checked" />
            </td>
            <td>
              <label for="course">Course</label>
              <select id="course" name="course[]">
                <option></option>
                <option>Course 1</option>
                <option>Course 2</option>
                <option>Course 3</option>
                <option>Course 4</option>
                <option>Course 5</option>
              </select> 
            </td>
            <td>
              <label for="year">Year</label>
              <select id="year" name="year[]">
                <option>Select Year</option> 
                <option>2000</option>
                <option>2001</option>
                <option>2002</option>
              </select>
            </td>
    
            <td>
              <label for="semester">Semester</label>
              <select id="semester" name="semester[]">
                <option>Select Semester</option> 
                <option>1</option>
                <option>2</option>
                <option>3</option>
              </select>
            </td>
          </tbody>
        </table>
        <p> 
        <input type="button" value="Add Course" onClick="addRow('dataTable')" /> 
        <input type="button" value="Remove Course" onClick="deleteRow('dataTable')" />
        <input id="button" name="submit" type="submit" value="Submit Data" />
    
      <p>
    
    </form>
    

    JS:一样。

    "data.php" 将收到一个多维数组。您可以通过以下方式查看结果:

    <?php
    echo '<pre>';
    print_r($_POST);
    echo '</pre>';
    ?>
    

    如果你填写 3 行,你会得到这个 $_POST 数组:

    Array
    (
        [chk] => Array
            (
                [0] => on
                [1] => on
                [2] => on
            )
    
        [course] => Array
            (
                [0] => Course 1
                [1] => Course 2
                [2] => Course 3
            )
    
        [year] => Array
            (
                [0] => 2000
                [1] => 2001
                [2] => 2002
            )
    
        [semester] => Array
            (
                [0] => 1
                [1] => 2
                [2] => 3
            )
    
        [submit] => Submit Data
    )
    

    【讨论】:

    • 非常感谢。它躲过了我
    • 不客气@SamolinaOne 请记住,如果有帮助,您可以投票并接受答案。
    • 拜托,我想知道上述变量(数组)如何在 FPDF 中显示。帮助将不胜感激。谢谢。
    猜你喜欢
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多