简体   繁体   English

JS创建的表单元素未提交

[英]JS-created form elements not submitting

I've got a form, with "regular" form elements, and then also form elements which are created by clicking a javascript button. 我有一个带有“常规”表单元素的表单,然后还有通过单击javascript按钮创建的表单元素。 Here's how it's called: 称呼为:

<script src="incld/addClass.js" language="Javascript" type="text/javascript">  
</script>  
<input type="button" value="Add a Class" onClick="addInput('dynamicInput');" class="add-class-btn">  

When I look at the $_POST output, though, these elements haven't been submitted. 但是,当我查看$ _POST输出时,这些元素尚未提交。

Here's the JS: 这是JS:

var counter = 1;
var limit = 25;
function addInput(divName){
  if (counter == limit)  {
     alert("You have reached the limit of adding " + counter + " inputs");
  } else {
     var newdiv = document.createElement('div');
     newdiv.innerHTML = "Class " + (counter + 1) + "          <p><label for=\"class_name\">Class Name </label><input type=\"text\" name=\"class[" + (counter + 1) + "][name]\" id=\"class[" + (counter + 1) + "][name]\" class=\"text-fld\" /></p><p><label for=\"class_grade_level\">Grade Level </label><input type=\"text\" name=\"class[" + (counter + 1) + "][grade_level]\" id=\"class[" + (counter + 1) + "][grade_level]\" class=\"text-fld\" /></p><p><label for=\"class_subject\">Subject </label><input type=\"text\" name=\"class[" + (counter + 1) + "][subject]\" id=\"class[" + (counter + 1) + "][subject]\" class=\"text-fld\" /></p>";
     document.getElementById(divName).appendChild(newdiv);
     counter++;
  }
}

Edit: The "regular" form elements are in a different div tag than the JS-created ones. 编辑:“常规”表单元素与JS创建的元素位于不同的div标签中。 Maybe this matters? 也许这很重要吗?

Edit2: Full (edited but ugly) source available here: http://pastebin.com/y5QJU4NN Edit2:完整(经过编辑但很丑陋)的源代码可以在这里找到: http//pastebin.com/y5QJU4NN

I was able to copy your code and create a form that submitted without changing it. 我能够复制您的代码并创建无需更改即可提交的表单。 However, on the receiving side after the form was submitted, I wound up with a POST structure similar to this: 但是,在提交表单之后,在接收方,我采用了类似于以下的POST结构:

<?php
    var_dump($_POST);
?>

array(1) { 
    ["class"]=> array(1) { 
        [2]=> array(3) { 
            ["name"]=> string(5) "test1" 
            ["grade_level"]=> string(5) "test2" 
            ["subject"]=> string(5) "test3" 
        } 
    } 
} 

So it looks like the code you posted above works. 因此,看起来您上面发布的代码有效。 Check to ensure that you are inspecting the right array level in the POST in your handler script. 检查以确保您正在处理程序脚本的POST中检查正确的数组级别。


Edit 编辑

It looks like PHP was not liking your use of array indices as field names. 看来PHP不喜欢您将数组索引用作字段名称。 I am sure you can specify a field name such as class[] and PHP will capture all incoming fields with field name of class[] as an array, but I am not sure you can specify a field name like class[1][name] . 我确定您可以指定一个字段名称,例如class[] ,PHP将捕获所有具有class[]字段名称的传入字段作为数组,但是我不确定您是否可以指定一个字段名称,例如class[1][name] I changed the field names in your JS to be something along the lines of: 我将您的JS中的字段名称更改为:

<input type=\"text\" name=\"class-" + (counter + 1) + "-name\" 
    id=\"class[" + (counter + 1) + "][name]\" class=\"text-fld\" />

And after submitting it was inserted into the POST just fine. 提交后将其插入POST就好了。 Try that and see if it works. 尝试一下,看看是否可行。

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

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