简体   繁体   English

如何从动态html表读取输入值?

[英]how to read the input values from a dynamic html table?

i have the following code to read the values from a regular input, 我有以下代码从常规输入中读取值,

HTML: HTML:

     <tr>
  <th>Email:</th>     
    <td><input id="email"  name="email" onchange="EmailValidate()" maxlength="50"></td>
  </tr>

PHP: PHP:

      file_put_contents($file, "\nEMAIL:", FILE_APPEND | LOCK_EX);

  $ret = file_put_contents($file, $_POST['email'], FILE_APPEND | LOCK_EX);

in the same way, i am trying to read the input values from a dynamically generated html table, but i am getting an error, 以同样的方式,我试图从动态生成的html表中读取输入值,但出现错误,

HTML: HTML:

     <table>
  <tr>
  <th>Number of Models:<title="Number of Models"></th>

  <td><select id="numbermodels" name="numbermodels"       onchange="buildTable(this.value); buildTable4(this.value);">

  <option value="1">1</option >
  <option value="2">2</option >
  <option value="3">3</option >
  <option value="4">4</option >
  <option value="5">5</option >
  <option value="6">6</option >
  </select></td>
  </tr>

  <tr>
  <th>Number of heads per model:</th>
  <td>
  <table id="contentTable" border="1" name="contentTable">
    <!-- Fill table programmatically -->
      </table></td>
  </tr>

Javascript: Javascript:

      function buildTable(val)
      {
     var myTable =document.getElementById("contentTable");
    var j=val;
        var rows = [];
    var cells = [];

     while (myTable.hasChildNodes()) {
     myTable.removeChild(myTable.lastChild);
      }


      for( var i = 0; i < 1; i++ )
       {
       rows[i] = myTable.insertRow(i);
       if(i%3==2)rows[i].addClass("every3rdrow");
         cells[i] = [];

       for( var x = 0; x < j ; x++ )
      {
        cells[i][x] =document.createElement((x==0)?"th":"td");
        cells[i][x].innerHTML = (x==0)?"<input id=t onchange=ty() name=t>":"<input     id=t onchange=ty()>";
        rows[rows.length - 1].appendChild(cells[i][x]);
        }
        }

         }
             buildTable();  

         function ty(){
      $ad = document.getElementById("t").value;
      if(!/^-?\d*$/.test($ad)) {
       alert("Number of heads per model value must be numeric!");
       } 
       } 

for this also i wrote the similar PHP code, but i am getting the error Undefined index t, PHP: 为此,我也编写了类似的PHP代码,但是却收到错误未定义索引t,PHP:

      file_put_contents($file, "\n Number of Heads per Model:", FILE_APPEND | LOCK_EX);

$ret = file_put_contents($file, $_POST['t'], FILE_APPEND | LOCK_EX);

what is the reason for this, what modifications can be done to be able to successfully read the input values from the dynamic html table ? 这是什么原因,可以进行哪些修改才能从动态html表中成功读取输入值?

A few suggestions: 一些建议:

  1. Element IDs must be unique. 元素ID 必须是唯一的。 You shouldn't use "t" for each input ID. 您不应该为每个输入ID使用“ t”。 You could change it to id="t_$i_$x" or something like that to make it unique. 您可以将其更改为id="t_$i_$x"或类似的名称以使其唯一。

  2. You need to let your server side script know that t is an array. 您需要让服务器端脚本知道t是一个数组。 To get all of the values from the form submitted, change the name=t in the <input> element templates to name="t[]" . 要从提交的表单中获取所有值,请将<input>元素模板中的name=t更改为name="t[]"

  3. Step 2 will break your validation function. 第2步将破坏您的验证功能。 I would change onchange=ty() to onchange="ty(this)" . 我将onchange=ty()更改为onchange="ty(this)" You will then have to change your ty() function implementation to: 然后,您必须将ty()函数的实现更改为:

     function ty(el) { if(!/^-?\\d*$/.test(el.value)) { alert("Number of heads per model value must be numeric!"); } } 

    This consequently simplifies your function by removing the need to get the element again (you simply pass an instance of it in when you call it in the onchange event). 因此,这消除了再次获取该元素的需要,从而简化了您的函数(在onchange事件中调用它时,只需传递它的一个实例)。

  4. This isn't necessarily required, but it's a good practice to always enclose your HTML tag attribute values in quotations, as I've shown in my suggestions above. 不一定需要这样做,但是如我上面建议中所示,始终将HTML标记属性值括在引号中是一种好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM