简体   繁体   English

无法从javascript创建的其他文本框中收集数据

[英]Unable to collect data from additional textbox created by javascript

I'm trying to collect data entered inside textboxes wrapped in div within a form.I'm using session to gather an array of data passed by the submit button. 我正在尝试收集在表单中包裹在div中的文本框中输入的数据,我正在使用会话来收集提交按钮传递的数据数组。 However it only captures the very first row of data. 但是,它仅捕获第一行数据。 I believe data entered in textboxes created by javascript not recognised? 我相信在javascript创建的文本框中输入的数据无法识别吗?

HTML Entry 1 HTML输入1

   <input type="text" name="myInputs_d[]" size='5' style='margin:4px;'>D(mm)
   <input type="text" name="myInputs_d1[]" size='5' style='margin:4px;'>D1(mm)
   <input type="text" name="myInputs_bags[]" size='5' style='margin:4px;'>Bags
   <input type="text" name="myInputs_carton[]" size='5' style='margin:4px;'>Cartons

    </div>
    <input type="submit" value="submit" name="submit">
    </form>    
  <input type="button" value="Add another text input"  onClick="addInput('dynamicInput');">

javascript to create multiple textboxes javascript创建多个文本框

<script>
var counter = 1;
var limit = 10;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "Entry " + (counter + 1) + " <input type='text' name='myInputs_d[]' size='5' style='margin:5px;'>D(mm)<input type='text' name='myInputs_d1[]' size='5' style='margin:5px;'>D1(mm)<input type='text' name='myInputs_bags[]' size='5' style='margin:5px;'>Bags<input type='text' name='myInputs_carton[]' size='5' style='margin:5px;'>Cartons";
          document.getElementById(divName).appendChild(newdiv);

          counter++;
     }
}
</script>

Php to echo all the data entered php回显所有输入的数据

$_SESSION['myInputs_all'][]=array($_POST["myInputs_d"],$_POST["myInputs_d1"],$_POST["myInputs_bags"],$_POST["myInputs_carton"]);
print_r($_SESSION['myInputs_all']);
foreach ($myInputs_all  as $eachInput) 
{
  echo $eachInput . "<br>";

}

I might have entered three sets of records ,but when I print_r($_SESSION['myInputs_all']);..... it only shows the first record like this. 我可能已经输入了三组记录,但是当我print_r($ _ SESSION ['myInputs_all']); .....时,它仅显示这样的第一条记录。

Array ( [0] => Array ( [0] => 10 ) [1] => Array ( [0] => Array ( [0] => 10 ) [1] => Array ( [0] => 10 ) [2] => Array ( [0] => 20 ) [3] => Array ( [0] => 20 ) )

Try out this code 试试这个代码

JavaScript: JavaScript的:

var counter = 1;
var limit = 10;
function addInput(divName){

 if (counter == limit)  {
      alert("You have reached the limit of adding " + counter + " inputs");
 }
 else {
    var newDiv = document.createElement('div');
    var stringInput = "Entry " + (counter + 1) + " <input type='text'           name='myInputs_d[]' size='5' style='margin:5px;'>D(mm)<input type='text' name='myInputs_d1[]' size='5' style='margin:5px;'>D1(mm)<input type='text' name='myInputs_bags[]' size='5' style='margin:5px;'>Bags<input type='text' name='myInputs_carton[]' size='5' style='margin:5px;'>Cartons";
    newDiv.innerHTML = stringInput;
    document.getElementById(divName).appendChild(newDiv);
    counter++;
 }
}

Your HTML should look like this. 您的HTML应该如下所示。 ( Add form tag and submit button ) (添加表单标签并提交按钮)

<form name="test" method="post" action="">
<div id="dynamicInput">

Entry 1

<input type="text" name="myInputs_d[]" size='5' style='margin:4px;'>D(mm)
<input type="text" name="myInputs_d1[]" size='5' style='margin:4px;'>D1(mm)
<input type="text" name="myInputs_bags[]" size='5' style='margin:4px;'>Bags
<input type="text" name="myInputs_carton[]" size='5' style='margin:4px;'>Cartons

</div>

<input type="button" value="Add another text input"  onClick="addInput('dynamicInput');">
<input type="submit" name="submit" value="Submit"  />
</form>

and in Last your php code should look like this: 最后,您的php代码应如下所示:

if( isset($_POST['submit']) ){

$_SESSION['myInputs_all'] = array($_POST["myInputs_d"],$_POST["myInputs_d1"],$_POST["myInputs_bags"],$_POST["myInputs_carton"]);

print_r($_SESSION['myInputs_all']);
echo "<br>";
foreach( $_SESSION['myInputs_all'] as $all_inputs ){

    foreach( $all_inputs as $inputs ){
        echo $inputs.'<br>';
    }

}
}

Your loop is not properly iterating over all elements, here is my suggestion: 您的循环未正确遍历所有元素,这是我的建议:

for($i = 0; $i < count($myInputs_all); ++$i) {
    for($j = 0; $j < (count($myInputs_all[$i])-1); ++$j) {
        for($k = 0; $k < 4; ++$k) {
            print "i:$i; j:$j; k:$k";
            print $myInputs_all[$i][$k][$j] . "<br>";
        }
    }
}

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

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