-
-
Notifications
You must be signed in to change notification settings - Fork 0
FormData and URLSearchParams
FormData and URLSearchParams are based on the browser APIs of the same names.
They are both key/value stores, but they represent different kinds of data:
-
FormDatais for form submission style body data, including files and repeated fields -
URLSearchParamsis for query-string style data
use GT\Http\FormData;
$formData = new FormData();
$formData->append("name", "Cody");
$formData->append("interest", "Mathematics");
$formData->append("interest", "Cryptography");
echo (string)$formData;
// name=Cody&interest[]=Mathematics&interest[]=CryptographyFormData can be created from a real DOM <form> element. When using PHP.GT/Dom this gives you a full modern HTMLDocument, just like in the browser:
use GT\Dom\HTMLDocument;
use GT\Http\FormData;
$html = <<<HTML
<form class="example-form">
<input name="name" value="Cody" />
<textarea name="message">Hello</textarea>
<button name="do" value="send">Send message</button>
</form>
HTML;
$document = new HTMLDocument($html);
$form = $document->querySelector(".example-form");
$formData = new FormData($form);The constructor reads named HTMLInputElements from the form.
Just like when using FormData in JavaScript, buttons are not included automatically, because only the clicked submitter should contribute its value during a real form submission.
If you want that behaviour, pass the submitter element as the second argument:
$formData = new FormData($form, $button);If the submitter is not part of the form, the constructor throws HttpException.
FormData can hold:
- strings
BlobFileSplFileObject
If you pass an SplFileObject, the library wraps it in a File object for you.
$formData->get("name");
$formData->getAll("interest");
$formData->getFile("avatar");
$formData->getBlob("attachment");use GT\Http\URLSearchParams;
$params = new URLSearchParams("?page=2&sort=name");
echo $params->get("page"); // 2
echo $params->size; // 2The leading ? is optional and ignored.
$params = new URLSearchParams([
"page" => "2",
"sort" => "name",
]);$params = new URLSearchParams($formData);This is useful when you want query-string style output from data you already gathered as form fields.
$params->append("filter", "new");
$params->append("filter", "popular");
$params->set("page", "3");
$params->delete("sort");Repeated keys are preserved and serialised with [].
URLSearchParams and FormData both inherit helpers from KeyValuePairStore, including:
entries()forEach()get()getAll()has()keys()values()delete()sort()
That means once you learn one of these classes, the other feels familiar.
Both classes also support the nullable type-safe getter helpers used elsewhere in PHP.GT.
For example:
$age = $params->getInt("age");
$name = $formData->getString("name");Use FormData when:
- you are representing a form submission
- repeated fields matter
- file values may be present
Use URLSearchParams when:
- you are building or reading a query string
- the data belongs in the URI
- you want a compact URL-encoded string
The next page covers the body helpers these classes often work alongside: Streams, blobs, and binary data.
PHP.GT/Http is a separately maintained component of PHP.GT/WebEngine.
- Creating requests from global state
- Request and ServerRequest objects
- Response objects
- URI objects
- Working with headers
- Parts of the HTTP header