0

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!

2
  • 1
    Your if(phone[... should be if($phone[... i presume? Commented May 24, 2010 at 7:02
  • Crap, thanks for the feedback. In my code it is '$phone'... Sorry, and thanks! Commented May 24, 2010 at 10:07

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

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.