Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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
Php 为foreach()循环提供的参数无效_Php_Foreach - Fatal编程技术网

Php 为foreach()循环提供的参数无效

Php 为foreach()循环提供的参数无效,php,foreach,Php,Foreach,嗨,我希望你们今天都很好 我似乎遇到了一个问题,恳请您的帮助 基本上,我使用jQuery来操作表单,表单然后存储在会话中,当返回到页面时,使用foreach循环将值从会话返回到页面中 问题是,如果我创建了这个foreach循环的多个实例,它会给我一个无效的参数(如果我反复添加相同的代码,它就可以正常工作) 首先,这里是代码 jQuery $(function(){ $('.add_child').click(function(){ var attrName = $(this).a

嗨,我希望你们今天都很好

我似乎遇到了一个问题,恳请您的帮助

基本上,我使用jQuery来操作表单,表单然后存储在会话中,当返回到页面时,使用foreach循环将值从会话返回到页面中

问题是,如果我创建了这个foreach循环的多个实例,它会给我一个无效的参数(如果我反复添加相同的代码,它就可以正常工作)

首先,这里是代码

jQuery

    $(function(){
$('.add_child').click(function(){
    var attrName = $(this).attr('name');
    var count = $(this).attr('count');
    $(this).attr('count', (parseInt(count)+1))
    var input = $('<input />');
    var lineBreak = $('<br/>');
    input.attr('type','text')
    input.attr('name',attrName+"["+count+"]" ) 
    $('.children_form').append(input);
    $('.children_form').append(lineBreak);
})
$('.add_s_child').click(function(){
    var attrName = $(this).attr('name');
    var count = $(this).attr('count');
    $(this).attr('count', (parseInt(count)+1))
    var input = $('<input />');
    var lineBreak = $('<br/>');
    input.attr('type','text')
    input.attr('name',attrName+"["+count+"]" ) 
    $('.spouce_children').append(input);
    $('.spouce_children').append(lineBreak);
})
});
$(函数(){
$('.add_child')。单击(函数(){
var attrName=$(this.attr('name');
var count=$(this.attr('count');
$(this.attr('count',(parseInt(count)+1))
变量输入=$('');
var lineBreak=$(“
”); input.attr('type','text') input.attr('name',attrName+“[”+计数+“])) $('.children_form')。追加(输入); $('.children_form')。追加(换行符); }) $('.add_s_child')。单击(函数(){ var attrName=$(this.attr('name'); var count=$(this.attr('count'); $(this.attr('count',(parseInt(count)+1)) 变量输入=$(''); var lineBreak=$(“
”); input.attr('type','text') input.attr('name',attrName+“[”+计数+“])) $('.spoute_children')。追加(输入); $('.spoute_children')。追加(换行符); }) });
以及PHP/HTML代码->按钮一(添加子项)

孩子们,请说出你的亲生孩子和/或另类孩子的名字


以及PHP/HTML代码->按钮二(喷动儿童)

spooces儿童,请说出所有自然儿童和添加儿童的名字
我想感谢你能/将要提供的任何帮助 @Xavier:你可以试试--



您的代码中哪一行出现错误?我猜你的会话并不总是初始化的,所以你得到了一个无效的参数,因为它不存在是的,很抱歉我这里有一个错误,我会编辑上面的你知道它的拼写是“配偶”?是的,我知道lol,我不担心这个阶段的拼写你在页面顶部有一个
start\u session()
?执行
print\r($\u SESSION['spaycechild'])
<?php foreach($_SESSION['children'] as $index=>$child){ ?>
<?php echo "<input type='text' name='children[{$index}]' value='{$child}'/>";?>
<?php } ?>

<input type="button" count='<?php echo count($_SESSION['children']);?>' name="children" class="add_child" value="Add Another Child"/>

<div class="children_form">
<?php //add the inputs here?>   
</div>
<label>Spouces Children, please name all natural and addopted children</label>
<input type="text" name="spoucechild" id="spoucechild" />

<?php foreach($_SESSION['spoucechild'] as $index=>$child){ ?>
<?php echo "<input type='text' name='spoucechild[{$index}]' value='{$child}'/>";?>
<?php } ?>

<input type="button" count='<?php echo count($_SESSION['spoucechild']);?>' name="spoucechild" class="add_s_child" value="Add Another Child"/>
<div class="spouce_children">
    <?php //add the inputs here?>     
</div>
<?php foreach($_SESSION['spoucechild'] as $index=>$child){ ?>
<?php
if (isset($_SESSION['spoucechild'])) {
    foreach($_SESSION['spoucechild'] as $index=>$child){
        echo '<input type="text" name="spoucechild[' . $index . ']" value="' . $child . '" />' . "\n";
    }
}
?>