2

I have the following Fiddle set up here Fiddle

As you can see, I am able to add inputs by clicking the Add Row button.

All inputs that are added have a unique id and name. The problem is, I cant just do something like

$actionInput = $_POST["actionInput"]; 

Because I might need

 $actionInput1 = $_POST["actionInput1"]; 
 $actionInput2 = $_POST["actionInput2"]; 
 $actionInput3 = $_POST["actionInput3"]; 

Depending on how many rows are added. So how can I get all the inputs without knowing what inputs I need to grab?

Thanks

5 Answers 5

1

Actually, you need to maintain counter in hidden, which you will get at the time of posting the form in case you don't want to maintain elements as array, otherwise you can put elements as array as described below:

<input type=text name="inputs[]" />
Sign up to request clarification or add additional context in comments.

Comments

1

Name your inputs with array boundary, like:

<input type=text name="actioninput[]" />

now you can itreate throught them in you POST or GET ( depends ) array:

print_r($_POST);

Comments

1

Assuming your JSFiddle works fine for you, following are the steps.

1) Get keys of $_POST.

2) Get maximum counter value from keys.

3) Take a for loop from 0 to count of post.

4) If counter is 0, no suffix, else, add counter as suffix.

5) Now, you get posted variable.

6) Repeat it for every element in rows.

<?php
$keys = array_keys($_POST);
$keys = implode(',', $keys);
$n = str_replace('actionInput', '', $keys);
$m = explode(',', $n);
$max = max($m);

for ($i=0 ; $i<=$max ; $i++) {
    $suffix = ($i==0) ? '' : $i;
    if (isset($_POST['actionInput' . $suffix])) {
        echo "<br/>-".$_POST['actionInput' . $suffix];
    }
}

1 Comment

This is not what I would recommend. This data is better handled as POST subarrays.
1

replace name=actionInput with name=actionInput[] it should be an array with same name same thing will apply with all form fields generated dynamically whose values you'll need.

Comments

0

Change this line in Add row jquery event return name + i to return name

Remove i from it and then name=actionInput[]

print_r($_POST);

It will gives array of actioninput

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.