dongqiaochi2711 2015-12-10 11:42
浏览 21
已采纳

POST数据未推送到POST数组

I am saving content items with a title and content value in a database. In my example you can see that I have one item for creating a new content item and the earlier saved items.

I save the items in a multidimensional array by adding the index in each input's name field like <input name="extra[2][title]"> (see example).

For the creating fields I try to store a new item with a new key by doing <input name="extra[][title]">, which normally pushes a new item to the end of an array. But when I print out the POST data the new item is not there, so I cannot store it in the array with the other items.

HTML FORM:

<form method="post">

    <!-- This is for creating a new item -->
    <div class="item">
        <input type="text" name="extra[][title]">
        <input type="text" name="extra[][content]">
    </div><!--End .item-->


    <!-- Items from array -->
    <?php
    $extras = array( /* Earlier saved items */ );
    foreach( $extras as $index => $extra ) { ?>

        <div class="item">
            <input type="text" name="extra[<?php echo $index; ?>][title]">
            <input type="text" name="extra[<?php echo $index; ?>][content]">
        </div><!--End .item-->

    <?php   
    }
    ?>

    <input type="submit" name="submit">

</form>

After doing this, the new item is not there:

<?php
if( ! empty( $_POST['extra'] ) ) {

    echo '<pre>';
    print_r( $_POST['extra'] );
    echo '</pre>';

}
?>

print_r( $_POST ) returns:

Array
(
    [extra] => Array
        (
            [0] => Array
                (
                    [title] => Test 1
                    [content] => test content
                )

            [1] => Array
                (
                    [content] => test content
                    [title] => Test 2
                )

            [2] => Array
                (
                    [title] => Test 3
                    [content] => test content
                )

        )

    [submit] => submit
)
  • 写回答

3条回答 默认 最新

  • dongyunque2511 2015-12-10 12:44
    关注

    With the help of RiggsFolly, I created this solution, which is very simple.

    I changed the form html to this:

    <form method="post">
    
        <!-- This is for creating a new item -->
        <div class="item">
            <input type="text" name="extra_title[]">
            <input type="text" name="extra_content[]">
        </div><!--End .item-->
    
    
        <!-- Items from array -->
        <?php
        $extras = array( 
            array(
                'title' => 'Test 1',
                'content' => 'test content'
            ),
            array(
                'title' => 'Test 2',
                'content' => 'test content'
            ),
            array(
                'title' => 'Test 3',
                'content' => 'test content'
            )
    
    
        );
        foreach( $extras as $index => $extra ) { ?>
    
            <div class="item">
                <input type="text" name="extra_content[]" value="<?php echo $extra['title']; ?>">
                <input type="text" name="extra_title[]" value="<?php echo $extra['content']; ?>">
            </div><!--End .item-->
    
        <?php   
        }
        ?>
    
        <input type="submit" name="submit" value="submit">
    
    </form>
    

    The POST outputs this:

    Array
    (
        [extra_title] => Array
            (
                [0] => Test 4
                [1] => Test 1
                [2] => Test 2
                [3] => Test 3
            )
    
        [extra_content] => Array
            (
                [0] => Tkldmsd
                [1] => test content
                [2] => test content
                [3] => test content
            )
    
        [submit] => submit
    )
    

    And then you can combine those values by index as follows:

    <?php
    if( ! empty( $_POST ) ) {
    
        echo '<pre>';
        print_r( $_POST );
        echo '</pre>';
    
        if( ! empty( $_POST['extra_title'] ) && ! empty( $_POST['extra_content'] ) ) {
    
            $extra = array();
    
            foreach( $_POST['extra_title'] as $index => $title ) {
    
                $extra[$index] = array(
                    'title' => $title,
                    'content' => $_POST['extra_content'][$index]
                );
    
            }
    
    
        }
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿