Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用FOREACH命令将添加的文本字段输出到PHP中_Php_Jquery_Dynamic_Foreach - Fatal编程技术网

如何使用FOREACH命令将添加的文本字段输出到PHP中

如何使用FOREACH命令将添加的文本字段输出到PHP中,php,jquery,dynamic,foreach,Php,Jquery,Dynamic,Foreach,这是我用来动态添加/删除输入文本字段的脚本。我正在使用容器属性,以便可以为该输入字段创建多个单独的块 $(function(){ $('.container > a').click(function(e){ e.preventDefault(); var $this= $(this), prnt = $this.parent(), i = prnt.find('input').length;

这是我用来动态添加/删除输入文本字段的脚本。我正在使用容器属性,以便可以为该输入字段创建多个单独的块

$(function(){
    $('.container > a').click(function(e){
        e.preventDefault();
        var $this= $(this),
            prnt = $this.parent(),
            i = prnt.find('input').length;
        if($this.hasClass('add')){
            $('<div><input type="text" class="field" name="dynamic[]" value="' + i + '" /></div>').hide().fadeIn('slow').appendTo($('.inputs',prnt));
        }else if($this.hasClass('remove') && i > 1){
            prnt.find('input.field:last').remove();
        }else if($this.hasClass('reset') && i > 1){
            prnt.find('input.field:gt(0)').remove();
        }
    });
})
$(函数(){
$('.container>a')。单击(函数(e){
e、 预防默认值();
变量$this=$(this),
prnt=$this.parent(),
i=prnt.find('input')。长度;
if($this.hasClass('add')){
$('').hide().fadeIn('slow').appendTo($('.inputs',prnt));
}else if($this.hasClass('remove')&&i>1){
find('input.field:last').remove();
}else if($this.hasClass('reset')&&i>1){
prnt.find('input.field:gt(0)').remove();
}
});
})
这是我用来构建输入字段的html代码:

<div class="container">
<a href="#" class="add">Add</a> | <a href="#" class="remove">Remove</a> | <a href="#" class="reset">reset</a>
    <div class="inputs">
        <input type="text" name="dynamic1" class="field"/>
    </div>
</div>

|  | 
我需要一个PHP代码来输出输入字段中的所有字段。如果我添加3个输入字段,我希望PHP代码一个接一个地输出所有这些输入字段

我尝试使用foreach代码:

<?php
foreach($_POST['dynamic[]'] as $value) {
echo "$value <br />"; // change this to what you want to do with the data
}
?>

但我在第二行(foreach行)上不断出错

我该怎么做?请帮忙

你应该改变

foreach($_POST['dynamic[]'] as $value) {

这是如果你有这样的HTML

<input type="text" name="dynamic[]" class="field"/>
<input type="text" name="dynamic[]" class="field"/>
echo $_POST['dynamic1'] ;

不要使用
$\u POST['dynamic[]”
试试
$\u POST['dynamic']

<?php
foreach($_POST['dynamic'] as $value) {
echo "$value <br />"; // change this to what you want to do with the data
}
?>

这适用于第二个字段,但它无法捕获第一个(初始)字段的数据!如何使第一个字段的数据也显示出来?请帮助!
<?php
foreach($_POST['dynamic'] as $value) {
echo "$value <br />"; // change this to what you want to do with the data
}
?>