1

I have a few dynamicaly build input fields in a form that looks like this:

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

<input type="text" class="form-control" name="person[]" placeholder="Person" />
<input type="text" class="form-control" name="function[]" placeholder="Function" />

Because i write my input name like this: person[] & function[] i get an numeric indexed array in my $_POST array. Because the input fields are made dynamicaly it is never known for sure how many person[] & function[]index numbers there will be in the $_POST.

How do i handle this? In other words what if i would like to INSERT person[0] + function[0] & person[1] + function[1] + ... (variable amount of person's & their functions) into my database? how could i extract this so it is possible to do this?

NOTE: Every person has his own function so it is important that it stays together.

I did not got very far so i cannot provide more code i have tried:

foreach ($_POST['person'] as $value){
...
} 

This gives me a list of that specific array person[] inside of the $_POST but this isn't usefull to me..

5
  • have you tried using php's built in count function? Commented Oct 7, 2014 at 12:17
  • Simply use the count function, or use foreach loop... Commented Oct 7, 2014 at 12:18
  • are these inputs dynamic, meaning there is a button + add more that involves javascript? Commented Oct 7, 2014 at 12:18
  • @Ghost it is with a + add more that i have implemented using jQuery Commented Oct 7, 2014 at 12:24
  • @Peter if you are using jQuery, I would create new IDs for each person/function pair. You can be certain that the IDs and users are paired in that case. Commented Oct 7, 2014 at 13:05

3 Answers 3

2

It sounds like there are the same number of 'people' as there are 'functions'. If this is the case, then you can do something like this:

foreach($_POST['person'] as $key => $value) {
    echo 'Person ' . $value . ' has function ' . $_POST['function'][$key] . '<br>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

you could add: if(count($_POST['person'])==count($_POST['function']){...
1

This form design will lead to problems if you don't have a way to ensure that the person and the function inputs are tied together in some way. Imagine someone is filling in the form, and they leave the function blank for one person:

Person    Function

Anna      array_map
Betty
Claire    count
          date_format

Rather than knowing that Betty's function is blank, the form logic will pair up Betty with count and Claire with date_format. It is therefore better to group each person/function combination with an ID so you can distinguish them. It is easy enough to do with an iterator in either JS or PHP.

New form:

<input type="text" class="form-control" name="persfunc[1][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[1][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[2][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[2][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[3][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[3][f]" placeholder="Function" />

<input type="text" class="form-control" name="persfunc[4][p]" placeholder="Person" />
<input type="text" class="form-control" name="persfunc[4][f]" placeholder="Function" />

You can then use a function like this to process the data:

    # gather up all the 'persfunc' data from $_POST
    if (! empty($_POST['persfunc'])) {
        # initialise an array for the results
        $pers_func_arr = array();

        foreach ($_POST['persfunc'] as $pf) {
            # make sure that both $persfunc[x]['p'] and $persfunc[x]['f'] have values
            if (
                isset($pf['p'])
                && isset($pf['f'])
                && strlen($pf['p']) > 0
                && strlen($pf['f']) > 0
            ) {
                # you've got a pair! Do whatever you want with them.

                # e.g. print out the name/function pair:
                echo "<p>Person " . $pf["p"]
                    . " has function " . $pf["f"]."</p>";

                # e.g. add them to a data structure for later use
                $pers_func_arr[] = array( $pf["p"], $pf["f"] );
            }
        }
    }

Give this form the dodgy input data from before, and check the output:

Person Anna has function array_map

Missing full data for person_2

Person Claire has function count

Missing full data for person_person_4

This is a much more robust way to handle the form data than just trusting your inputs will match up, and it doesn't require a lot more coding.

Comments

0

Something like this would probably work

$sql = "INSERT INTO tbl (Person,Function) VALUES ";
$components = array();

$totalitemcount = count($_POST['person']);

for ($i=0;$i<=$totalitemcount;$i++) {
    $components[] = "('".$_POST['person'][$i]."','".$_POST['function'][$i]."')";
}

$insertstring = implode(",",$components);

$finalsql = $sql.$insertstring;

//do insert here

3 Comments

you should note that there is absolutely no escaping of data, and it is prone to sql injection
aye forgot to mention that bit :) user should really do that themselves or ideally use a pdo/mysqli prepare etc.
I will give it a try, look what is best and come back! @Sharky i am familiar with PDO and prepared statements & binds ;) so that is not the issue. Thanks anyway!

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.