简体   繁体   English

表单只发布第一个动态生成的输入

[英]Form only posting first dynamically generated inputs

I have a form which have three different input elements.我有一个包含三个不同输入元素的表单。 Each element child element is created by jquery dynamically.每个元素的子元素都是由 jquery 动态创建的。 My problem is, when I try to submit my form (in php) it post only first dynamic input value second and third dynamic input values are not posting.我的问题是,当我尝试提交我的表单(在 php 中)时,它只发布第一个动态输入值,第二个和第三个动态输入值没有发布。

<input type="text" name="first[]"><input type="button" id="first" value="first>
<div class="warp1></div>
<input type="text" name="second[]"><input type="button" id="second" value="second>
<div class="warp2></div>
<input type="text" name="third[]"><input type="button" id="third" value="third>
<div class="warp3></div>

jquery code: jQuery代码:

var wrap1  = $(".wrap1");
var btn1   = $("#first"); 
 $(btn1).click(function(e){ 
    e.preventDefault();
 var appendText='<input type="text" name="first[]" />';
        $(wrap1).append(appendText); 

});

similar for other button

php code代码

print_r $_POST['first'];
print_r $_POST['second'];
print_r $_POST['third'];

If I add two-two dynamic element in every field then the result may like this array([0]=>,[1]=>,[2]=>) but this show for first element second and third element result is array([0]=>)如果我在每个字段中添加两到两个动态元素,那么结果可能像这个array([0]=>,[1]=>,[2]=>)但是这个显示第一个元素的第二个和第三个元素结果是array([0]=>)

You have a few problems in the code you posted.您发布的代码中存在一些问题。 Think about the following code you wrote :想想你写的以下代码:

var btn1   = $("#first"); 
 $(btn1).click(function(e){ 
 //......

});

You took $("#first") in btn1 .您在btn1$("#first") So next it should be btn1.click(function(e){ instead of $(btn1).click(function(e){ . Same thing applies for $(".warp1") ;所以接下来应该是btn1.click(function(e){而不是$(btn1).click(function(e){ 。同样的事情适用于$(".warp1") ;

Some problems in HTML were also there. HTML 中也存在一些问题。 The value attributes were not properly enclosed with commas. value属性未正确用逗号括起来。

JS: JS:

 $(document).ready(function () {

            var warp1  = $(".warp1");
            var btn1   = $("#first");
            btn1.click(function(e){
                e.preventDefault();
                var appendText='<input type="text" name="first[]" />';
                warp1.append(appendText);

            });



            var warp2  = $(".warp2");
            var btn2   = $("#second");
            btn2.click(function(e){
                e.preventDefault();
                var appendText='<input type="text" name="second[]" />';
                warp2.append(appendText);

            });

            var warp3  = $(".warp3");
            var btn3   = $("#third");
            btn3.click(function(e){
                e.preventDefault();
                var appendText='<input type="text" name="third[]" />';
                warp3.append(appendText);

        });

        });

HTML and PHP together in same page: HTML 和 PHP 在同一个页面中:

<?php

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


print_r ($_POST['first']);

echo "<br/>";
print_r ($_POST['second']);

echo "<br/>";
print_r ($_POST['third']);

}

?>
<form action="" method="post">

<input type="text" name="first[]"><input type="button" id="first" value="first" />
<div class="warp1"></div>
<input type="text" name="second[]"><input type="button" id="second" value="second" />
<div class="warp2"></div>
<input type="text" name="third[]"><input type="button" id="third" value="third" />
<div class="warp3"></div>

<input type="submit" value="Submit" name="submit" />
</form>

I added the form which you did not include in OP.我添加了您未包含在 OP 中的form The form with empty value for action attribute submits to the same page. action属性值为空的表单提交到同一页面。 You can however use a value ie action="somepage.php" to submit to a different page.但是,您可以使用值,即action="somepage.php"提交到不同的页面。

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

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