docs:browsers:posting_forms

This is an old revision of the document!


posting forms

When posting a web form, various things can occur that are non-standard or unexpected.

Actually, the first submittable object on the form is activated. To control which element this is, add the attribute 'default'.

<input type="submit" name="submit" default="1" value="Submit" />

Be careful checking for the submit key in the $_POST array because IE will not send it unless the user actually clicks the submit button, or presses enter on the button. If the user presses enter in a text field, the 'submit' key will not be set in the $_POST array.

FIXME

To have the selections of a select element become an array when the form is posted, use [] in the variable name.

<select name="items[]" multiple size="3">
  <option value="1">One</option>
  <option value="2">One</option>
  <option value="3">One</option>
</select>

One feature of PHP's processing of POST and GET variables is that it automatically decodes indexed form variable names.

I've seem innumerable projects that jump through extra & un-needed processing hoops to decode variables when PHP does it all for you:

Example pseudo code:

Many web sites do this:

<form ....>
<input name="person_0_first_name" value="john" />
<input name="person_0_last_name" value="smith" />
...
 
<input name="person_1_first_name" value="jane" />
<input name="person_1_last_name" value="jones" />
</form>

When they could do this:

<form ....>
<input name="person[0][first_name]" value="john" />
<input name="person[0][last_name]" value="smith" />
...
<input name="person[1][first_name]" value="jane" />
<input name="person[1][last_name]" value="jones" />
</form>

With the first example you'd have to do string parsing / regexes to get the correct values out so they can be married with other data in your app… whereas with the second example.. you will end up with something like:

<?php
var_dump($_POST['person']);
//will get you something like:
array (
0 => array('first_name'=>'john','last_name'=>'smith'),
1 => array('first_name'=>'jane','last_name'=>'jones'),
)
?>

This is invaluable when you want to link various posted form data to other hashes on the server side, when you need to store posted data in separate “compartment” arrays or when you want to link your POSTed data into different record handlers in various Frameworks.

Remember also that using [] as in index will cause a sequential numeric array to be created once the data is posted, so sometimes it's better to define your indexes explicitly.

  • docs/browsers/posting_forms.1245794115.txt.gz
  • Last modified: 2009/06/23 15:55
  • by billh