1

Right, when users submit a form to update their contact information... odd things happen that make very little sense to me, and make it impossible for me to properly parse the resulting data.

The $_POST data sent to my script (found via print_r) is as follows:

Array
(
    [name_first] => Charles
    [name_last] => Broughton
    [company] => 
    [address1] => 
    [address2] => 
    [city] => Bristol
    [region] => England
    [postal] => 
    [country] => 1
    [email] => *******************
    [phones_types] => Array
        (
            [0] => Cell
        )

    [phones_numbers] => Array
        (
            [0] => ************
        )

    [phone_types] => Array
        (
            [1] => Home
        )

    [phone_numbers] => Array
        (
            [1] => ************
        )

    [pass] => **********
    [id] => 
)

The form creating this odd output is as follows:

<form action="URL" method="POST">
  <table class="data" align="center" border="0" cellpadding="10" cellspacing="0" width="100%"><tbody>
    <tr>
      <th colspan="3">Edit Contact</th>
    </tr>
    <tr>
      <td>First Name:</td>
      <td><input type="text" name="name_first" value="Charles"/> (required)</td>
    </tr>
    <tr>
      <td>Last Name:</td>
      <td><input type="text" name="name_last" value="Broughton"/></td>
    </tr>
    <tr>
      <td>Company:</td>
      <td><input type="text" name="company" value=""/></td>
    </tr>
    <tr>
      <td>Address Line 1:</td>
      <td><input type="text" name="address1" value=""/></td>
    </tr>
    <tr>
      <td>Address Line 2:</td>
      <td><input type="text" name="address2" value=""/></td>
    </tr>
    <tr>
      <td>City:</td>
      <td><input type="text" name="city" value="Bristol"/> (required)</td>
    </tr>
    <tr>
      <td>Region:</td>
      <td><input type="text" name="region" value="England"/> (required)</td>
    </tr>
    <tr>
      <td>Postal:</td>
      <td><input type="text" name="postal" value=""/></td>
    </tr>
    <tr>
      <td>Country:</td>
      <td><select name="country">
        <option value="1" selected="selected">United Kingdom</option>
        <option value="2">United States</option>
      </select> (required)</td>
    </tr>
    <tr>
      <td>Email:</td>
      <td><input type="text" name="email" value="*******************"/> (required)</td>
    </tr>
    <tr>
      <td>Phones(s):</td>
      <td><input type="text" name="phones_types[0]" value="Cell"/>: <input type="text" name="phones_numbers[0]" value="************"/><br/><input type="text" name="phone_types[1]"/>: <input type="text" name="phone_numbers[1]"/></td>
    </tr>
    <tr>
      <td>Current Password:</td>
      <td><input type="password" name="pass"/> (required)</td>
    </tr>
    <tr align="center">
      <td colspan="2"><input type="submit" value="Save Changed"/></td>
    </tr>
    <input type="hidden" name="id" value=""/>
  </tbody></table>
</form>

Does anybody know how I can remedy this, or parse it properly using PHP? I tried a for loop to parse both arrays as one, but neither of them are arrays due to the separation... the example of which is in the above print_r output.

--

EDIT I was attempting to parse the form data using the following bit of PHP, with the proceeding error message being outputted.

if (count($_POST['phone_types'])!=0 && count($_POST['phone_numbers'])!=0)
{
  for ($i = 0; $i < count($_POST['phone_types']); $i++)
  {
    if (!empty($_POST['phone_types'][$i]) && !empty($_POST['phone_numbers'][$i]))
      $phones[$_POST['phone_types'][$i]] = $_POST['phone_numbers'][$i];
  }
  $ph = "";
  foreach ($phones as $k => $v)
  {
    $ph.= "$k:$v;";
  }
  $phones = $ph;
}
else
  $phones = "";

Error:

Warning: Invalid argument supplied for `foreach()` in `FILE` on line 35
7
  • 2
    what are you expecting? that is what was posted if you do not change anything Commented Jul 18, 2011 at 22:13
  • I entered a second phone number and second type in the form when submitting it, and instead of getting two arrays of [0] => number, [1] => number... they are two different sets of two arrays due to... form input order I'm guessing? But then PHP doesn't understand it at all. Commented Jul 18, 2011 at 22:14
  • looks fine to me :/ what's the problem? Commented Jul 18, 2011 at 22:15
  • 1
    you have the namings of the inputs wrong (one is prural and one is singular) - if I understood your problem... Commented Jul 18, 2011 at 22:17
  • @cdbroughton -- use [] instead of [0], [1], ... Commented Jul 18, 2011 at 22:18

4 Answers 4

2

You're using array notation in the input fields: phones_types[0] indicates to PHP that it should create a phones_types array, and this particularly field should be element 0 in it.

Any form field with [] at the end of its name is treated by PHP as a directive to treat it as an array, so you can submit multiple values to the same 'name'.

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

2 Comments

I am aware of this, but it is creating a single array PER index, I'm guessing because of the order of inputs on the form. Which means when I do a for or foreach on it.... the output does not have two indexes, but only one instead.
then why not just change it to [] instead of indexed versions? Should work properly anyway
1
<input type="text" name="phones_types[0]" value="Cell"/>: 
<input type="text" name="phones_numbers[0]" value="************"/><br/>
<input type="text" name="phone_types[1]"/>: 
<input type="text" name="phone_numbers[1]"/>

should be:

<input type="text" name="phones_types[0]" value="Cell"/>: 
<input type="text" name="phones_numbers[0]" value="************"/><br/>
<input type="text" name="phones_types[1]"/>: 
<input type="text" name="phones_numbers[1]"/>    

in order to be able to loop the phones_types & phones_number using a foreach

1 Comment

Wow, I can't beleive I didn't see that.
1

Field names that have brackets in them are automatically converted to arrays by PHP. If you are having problems with missing array entries, try changing [0] to [] in your field names.

See http://www.php.net/manual/en/faq.html.php#faq.html.arrays.

1 Comment

I am aware of this, but it is creating a single array PER index, I'm guessing because of the order of inputs on the form. Which means when I do a for or foreach on it.... the output does not have two indexes, but only one instead.
0

One of your posts is for phones_numbers and the other is for phone_numbers

Those do no match so they will be separate _POST values

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.