1

I'm working on a form where I have 2 fields which can be added N number of times. I have 2 fields called "fieldA" and "fieldB" and a button called "addrow" . So as many times I click on addrow I add fieldA and fieldB beneath previous fieldA and fieldB. So I can have unlimited fieldAs and unlimited fieldBs. I use jquery to add rows and append new fields with number behind each field's name attr to make it unique. So first set of fields will be named as fieldA1, fieldB1 and second set will be named as fieldA2 , fieldB2 and so on.

This is how my part of my form looks

enter image description here

And here's my jquery

$(document).ready(function(){
 var $i = 1;
    $('body').on('click','#addrow',function(){
    $('#fieldset').append(
    "Your Friends Name: <br /><input type='text' name='fieldA"+$i+"' /><br /><br />Your Friends Email: <br /><input type='text' name='fieldB"+$i+"' /><br /><br />"
    );
    $i++;
    });
});

Now since these fields are generated automatically using jquery I'm not sure how to fetch field's value in php.

also after fetching field's values I would also like to create an array.

This is how I expect to save values of all my fields in php array.

$fieldset = array( 
array("fieldA" => "value of fieldA1","fieldB" => "value of fieldB1"),
array("fieldA" => "value of fieldA2","fieldB" => "value of fieldB2"),
array("fieldA" => "value of fieldA3","fieldB" => "value of fieldB3"),
...
);
0

2 Answers 2

4

You inputs must be array [], otherwise you will not be able to receive them as array fieldA[]

Your jQuery should be like this:

$(document).ready(function(){
 var $i = 1;
    $('body').on('click','#addrow',function(){
    $('#fieldset').append(
    "Your Friends Name: <br /><input type='text' name='fieldA["+$i+"]' /><br /><br />Your Friends Email: <br /><input type='text' name='fieldB["+$i+"]' /><br /><br />"
    );
    $i++;
    });
});

HTML

Your Friends Name: <br /><input type='text' name='fieldA[0]'  class='name'/><br /><br />
Your Friends Email: <br /><input type='text' name='fieldB[0]' class='email'/><br /><br />

and in your PHP you will receive them as

[fieldA] => Array (
                [0] => 
                [1] => 
                [2] => 
            )
[fieldB] => Array ( 
                [0] => 
                [1] => 
                [2] =>
            )

you can convert them as you want

$newArray = array();

for($i=0; $i<count($_POST['fieldA']); $i++){
    $newArray[$i]['fieldA'] = $_POST['fieldA'][$i];
    $newArray[$i]['fieldB'] = $_POST['fieldB'][$i];
}
Sign up to request clarification or add additional context in comments.

5 Comments

hi @Mihai . Alright I'm updating my js as you said. But in the meantime how should I fetch my fields in php? I mean using what code? $fieldA[] = $_REQUEST['fieldA[]'];
like I told you, last for I made will get you exactly the result you wanted. The field come as $_POST['fieldA'][0], $_POST['fieldA'][1] etc ..
And BTW I updated your $('#fieldset').append( "Your Friends Name: <br /><input type='text' name='fieldA["+$i+"]' /><br /><br />Your Friends Email: <br /><input type='text' name='fieldB["+$i+"]' /><br /><br />" );
Also I renamed my main html input tag's Name attr to. Your Friends Name: <br /><input type='text' name='fieldA[0]' /><br /><br />Your Friends Email: <br /><input type='text' name='fieldB[0]' />
So Shall I edit & update your answer ? For others who reach this question.. Thanks :)
0

if you're using POST to submit those fields in your form, you can do:

foreach ($_POST as $key => $value) {
    $fieldset[$key] => $value;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.