duanchuanqu593743 2017-09-04 15:01
浏览 55
已采纳

too long

HTML

In this the user can add up to 10 pairs of textfields on runtime; I'm using jQuery here.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        var max_fields      = 10;
        var wrapper         = $(".container1");
        var add_button      = $(".add_form_field");

        var x=1;
        $(add_button).click(function(e){
            e.preventDefault();
            if(x < max_fields){

                x++;
                $(wrapper).append('<div><input type="text" name="mytext[]"/></div><div><input type="text" name="mytext2[]"/><a href="#" class="delete">Delete</a></div>'); //add input box
            }
            else{
                alert('You Reached the limits');
            }
        });

        $(wrapper).on("click", ".delete", function(e){
            e.preventDefault(); $(this).parent('div').remove(); x--;
        });
    });
</script>

<form action="<?php base_url();?>index.php/Welcome/formdata" method="POST">
<div class="container1">
    <button class="add_form_field">Add New Field &nbsp; <span style="font-size:16px; font-weight:bold;">+ </span></button>
    <div><input type="text" name="mytext[]"></div>
    <div><input type="text" name="mytext2[]"></div>
</div>
<input type="submit" name="submit"/>
</form>

PHP

Here i am getting all the info the user entered. Because every time the number of input fields change I tried to use a foreach loop to to enter one group of data at a time. I am using CodeIgniter to insert the data into the database. I dont want to use a for loop in my model - that's why I need $data to be an array similar to this

$data = array(
    array(
        'mytext1' => 'xx', 
        'mytext2' => 'xxx'),
    ),
    array(
        'mytext1' => 'xx', 
        'mytext2' => 'xxx'),
    )
);

so that I can use insert_batch.

But array_merge() is giving me only first data after loop ended.

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller 
{
    public function index()
    {
        $this->load->view('welcome_message');
    }

    public function formdata()
    {
        $data = array();

        foreach($_POST as $post) {
            $data2['mytext'] = $post[0];
            $data2['mytext2'] = $post[1];

            $data = array_merge($data2, $data);
        }

        print_r($data);
        exit;
    }
}

It's not merging all the items its just give me the first input I enter in the form.

How can I fix this?

  • 写回答

1条回答 默认 最新

  • dousi9215 2017-09-04 15:20
    关注

    You can use array_map() instead:

    public function formdata()
    {
        $data = array_map(function ($myText, $myText2) {
            return [
                'mytext' => $myText,
                'mytext2' => $myText2,
            ];
        }, $_POST['mytext'], $_POST['mytext2']);
    
        print_r($data);
        exit;
    }
    

    Note This assumes that

    • $_POST['mytext'] is an array
    • $_POST['mytext2'] is an array
    • there will be as many elements in $_POST['mytext'] as there are in $_POST['mytext2'].

    For reference, see:

    For an example, see:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大