使用PHP在MongoDB中保存动态表行

时间:2015-06-18 14:32:26

标签: javascript php mongodb

我已经编写了一些JavaScript来动态地向<table>添加行。如何将动态生成的行中的数据保存到MongoDB数据库? 我添加了php代码来保存mongodb中的表。 有一个变量nextName,它增加name变量以帮助生成变量。 这是我的HTML和JavaScript:

nextName=2;
    function myCreateFunction(tableid) {
      var table = document.getElementById(tableid);
      var rowCount = table.rows.length;
      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[1].cells[i].innerHTML;
        switch (newcell.childNodes[0].type) {
          case "text":
            newcell.childNodes[0].value = "";
            newcell.childNodes[0].name = "name' + nextName + '";
            break;
          case "checkbox":
            newcell.childNodes[0].checked = false;
            break;
          case "select-one":
            newcell.childNodes[0].selectedIndex = 0;
            break;
        }nextName++;
      }

    }

    function myDeleteFunction(tableID) {
      try {
        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 delete all the rows.");
              break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
          }
        }
      } catch (e) {
        alert(e);
      }
    }
    <table border="1" id="myTable">

      <tr>
        <th>select</th>
        <th>Degree</th>
        <th>H.R.A.</th>
        <th>College/University</th>
        <th>Divn.</th>
        <th>%age/CGPA</th>
        <th>Remarks</th>
      </tr>

      <tr>
        <td><INPUT type="checkbox" name="chk" /></td>
        <td>
          <select id="ddeg" name="ddeg">
            <option value="degg1">P.H.D.</option>
            <option value="degg2">M.C.A.</option>
            <option value="degg3">B.Sc</option>
            <option value="degg4">B.E.</option>
            <option value="degg5">12th</option>
            <option value="degg6">10th</option>
          </select>
        </td>
        <td><input type="text" name="hran" /></td>
        <td><input type="text" name="college" /></td>
        <td><input type="text" name="divn" /></td>
        <td><input type="text" name="cg" /></td>
        <td><input type="text" name="remark" /></td>
      </tr>
      
    </table>

    <br>
    <button onclick="myCreateFunction('myTable')">Add row</button>
    <button onclick="myDeleteFunction('myTable')">Delete row</button>
    <br>
<?php session_start(); $m = new MongoClient(); $db=$m->form; $col=$db->mycol; $url=$target_file; $arr=array(); for($x=2;$x<=nextName;$x++) { $arr[$x]=$_POST["name' + $x + '"]; } $y=0; $new = array( foreach($arr as $value) { "tab' + $y + '"=>$value; $y++; } ); $col->insert($new); ?>

0 个答案:

没有答案