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?

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: