Skip to content
Advertisement

How to get data form Same with many elements as separate set for each input field?

I have a with 3 tags as given below;

 <input type="name" class="form-control" id="name" placeholder="Enter name" name="name[]">
 <input type="text" class="form-control" id="phone" placeholder="Enter phone" name="phone[]">
 <input type="file" name="Image[]" id="image"  multiple />

I enter record of 2 persons. I select 2 images for 1st person and 3 images for 2nd person. When I submit form then I receive data as following;

Array
(
    [name] => Array
        (
            [0] => Mr A
            [1] => Mr B
        )

    [phone] => Array
        (
            [0] => 1234567
            [1] => 9876543
        )

    [Image] => Array
        (
            [0] => 1 (1).png
            [1] => 1 (2).png
            [2] => 1 (3).png
            [3] => 1 (4).png
            [4] => 1 (5).png
        )

    [submit] => Submit
)

Problem is, How can I identify which images belongs to which person. I need data as separate set for each person as following

Array
(
    [name] => Array
        (
            [0] => Mr A
            [1] => Mr B
        )

    [phone] => Array
        (
            [0] => 1234567
            [1] => 9876543
        )

    [Image] => Array
        (
            [0] => Array
                        (
                            [0] => 1 (1).png
                            [1] => 1 (2).png
                        )
            [1] => Array
                        (
                            [0] => 1 (3).png
                            [1] => 1 (4).png
                            [2] => 1 (5).png
                        )
        )

    [submit] => Submit
)

Please help in this context

Advertisement

Answer

I have solved my problem myself. We could not send data as separate group unless we change name attribute. I have fixed by changing the name onClick

<input type="file" name="image[1][]" id="" onchange="countFiles()" multiple />

JQuery

function countFiles(){
 var n = $(':input[type=file]').length; //counts input type file tags
 $(event.currentTarget).attr('name', 'image['+n+'][]'); //changes name
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement