Core\Request::formField
Core\Request::formField(string $name): ?tainted stringCore\Request::formField( string $name, ): ?tainted string
Returns one field from a form that a browser sent with POST. You give the name of the field. The result is its value as a tainted string, which is text that came from the client. The result is null when the form has no field of that name. A checkbox that is not ticked is not sent, so its result is null. A field that was sent with no text gives an empty string.
The name must match exactly, and upper and lower case are different. A + in the value is returned as a space, and a percent escape such as %40 is decoded. A name with square brackets, such as item[]=pen, gives null. Read those values with Core\Request::formFields.
formField reads both kinds of form: application/x-www-form-urlencoded and multipart/form-data. If you also need the uploaded files, call Core\Request::files() first.
A command-line program has no request, so formField throws a LogicError there. When an escape in the form is not valid UTF-8 text, formField throws a RuntimeError.
The examples below read a contact form, show a field that is missing or empty, and show a field with brackets.
Parameters
$namestringThe field's name, as the form declared it. Upper and lower case are different.
Return value
Returns ?tainted string. The field's value as a tainted string, or null when the form has no field with this name. A name with square brackets, such as item[]=pen, also gives null. Read those values with formFields. This method reads the body to its end. On a multipart/form-data request, call it after files(), because it skips the uploads on its way to the last field.
Errors
LogicErrorThis program is not handling a request, orbodyorbodyStreamalready read the body of this request. CallingformFieldafterfiles()is allowed.ParseErrorThe body is not the form it says it is, or amultipart/form-databody has no boundary.RuntimeErrorThe name or value of a field is not valid UTF-8. In an urlencoded form, this includes a percent escape that decodes to bytes that are not UTF-8.IOErrorThe connection failed while the body was read, or the body ended before the length it declared.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Request::queryParameter, queryParameters and queryParametersAs read the query string. formField, formFields and formFieldsAs read a submitted form. The old names were query, queryArray, queryAs, post, postArray and postAs. |
Examples
Reading a contact form
<?nvs// Shows how to read the fields of a form that a browser sent with `POST`.
// `Core\Request::formField` returns the value of the field "email" as text.// `%40` in the body is the character `@`, so this prints "email: ada@example.com".?tainted string $email = Core\Request::formField('email');echo 'email: ', $email ?? '', "\n";
// A `+` in the body is a space. This prints "message: Please call me".?tainted string $message = Core\Request::formField('message');echo 'message: ', $message ?? '', "\n";email: ada@example.commessage: Please call meA checkbox that was not ticked
<?nvs// Shows what `Core\Request::formField` returns for a field that is missing or empty.
// A browser does not send a checkbox that is not ticked.// The body has no field "newsletter", so the result is `null`. This prints "newsletter: no".echo 'newsletter: ', Core\Request::formField('newsletter') == null ? 'no' : 'yes', "\n";
// The field "comment" was sent with no text. The result is an empty string, not `null`.// This prints "comment is empty".?tainted string $comment = Core\Request::formField('comment');echo $comment == '' ? "comment is empty\n" : 'comment: ' . $comment . "\n";newsletter: nocomment is emptyA field with brackets
<?nvs// Shows what `Core\Request::formField` returns for a field written with square brackets.
// The form sends `item[]=pen&item[]=book¬e=gift+wrap`.// "note" has no brackets, so this prints "note: gift wrap".echo 'note: ', Core\Request::formField('note') ?? 'none', "\n";
// "item" was sent with brackets, so `formField` returns `null` for it. This prints "item: none".echo 'item: ', Core\Request::formField('item') ?? 'none', "\n";
// `Core\Request::formFields` returns the values of "item". This prints "items: 2".echo 'items: ', Core\Arr::count(Core\Request::formFields('item')), "\n";note: gift wrapitem: noneitems: 2