tags:

views:

46

answers:

2

Hey everyone,

I am using PHP to create a form with an array of fields. Basically you can add an unlimited number of 'people' to the form and each person has a first name, last name, and phone number. The form requires that you add a phone number for the first person only. If you leave the phone number field blank on any others, the handler file is supposed to be programmed to use the phone number from the first person.

So, my fields are:

person[] - a hidden field with a value that is this person's primary key.

fname[] - an input field

lname[] - an input field

phone[] - an input field

My form handler looks like this:

$people = $_POST['person']
$counter = 0;

foreach($people as $person):
    if($phone[$counter] == '') {
    // use $phone[0]'s phone number
    } else {
    // use $phone[$counter] number
    }
    $counter = $counter + 1;
endforeach;

PHP doesn't like this though, it is throwing me an

Notice: Uninitialized string offset error.  

I debugged it by running the is_array function on people, fname, lname, and phone and it returns true to being an array. I can also manually echo out $phone[2], etc. and get the correct value. I've also ran is_int on the $counter variable and it returned true, so I'm unsure why this isn't working as intended?

Any help would be great!

+8  A: 

I am pretty sure phone[$counter] should be $phone[$counter]. Otherwise it will treat "phone" as a string.

nc3b
A: 

var_dump your $_POST value and see what's going on. The array indicies probably aren't set to what you're expecting.

Aaron