February 14, 2017 by Christoff Truter PHP
In this series of posts I am going to create a PHP drop-down list, I am going to follow an evolutionary approach (simple code to more complex, while explaining my thought processes along the way) and hopefully end up with something useful.
Observe the following snippet.<form method="POST"> <select name="friends"> <option>Not Selected</option> <option>Gerhardt Stander</option> <option>Bronwen Murdoch</option> <option>Maree Kleu</option> </select> <br> <input type="submit" value="Go"> </form>
The select element is the part we need to implement, along with its option (child) elements.
Now this is our starting point, we need to understand our markup first.
Select Element
In addition to the general HTML attributes, the following additional attributes are supported in the HTML spec, the rows highlighted in red were added in the HTML5 spec.
Attribute | Description |
---|---|
autofocus | Specifies that the drop-down list should automatically get focus when the page loads |
disabled | Specifies that a drop-down list should be disabled |
form | Defines one or more forms the select field belongs to |
multiple | Specifies that multiple options can be selected at once |
name | Defines a name for the drop-down list |
required | Specifies that the user is required to select a value before submitting the form |
size | Defines the number of visible options in a drop-down list |
HTML5 attributes are currently not fully supported by all major browsers, if we're interested in using them we'll need to supply some kind of crutch library (aka polyfills), for now we're going to give them a skip.
I am going to ignore the multiple attribute, since this is more related to list boxes and we're more interested in a drop-down list at this stage.
I am going to ignore the size attribute as well, as it is generally only relevant to list boxes, and I also felt its something more suited to the CSS domain, but apparently this attribute cannot be set via CSS (so if we ever get to the point where we're going to create a list box, this attribute will be included in those classes).
Representing this element using PHP would look something like this:
class HtmlSelectElement { public $Name; public $Disabled; public $Children; public function __construct($name, array $children = [], $disabled = false) { $this->Name = $name; $this->Disabled = $disabled; $this->Children = $children; } }
Attribute | Description |
---|---|
disabled | Specifies that an option should be disabled |
label | Specifies a shorter label for an option |
selected | Specifies that an option should be pre-selected when the page loads |
value | Specifies the value to be sent to a server |
class HtmlOptionElement { public $Disabled; public $Selected; public $Value; public $Text; public function __construct($text, $value = null, $selected = false, $disabled = false) { $this->Text = $text; $this->Value = $value; $this->Selected = $selected; $this->Disabled = $disabled; } }
February 17, 2017
PHP drop-down list - Part 5 (Custom Serialization)February 15, 2017
PHP drop-down list - Part 4 (Cleaning things up a bit)February 14, 2017
PHP drop-down list - Part 3 (Maintaining State)February 14, 2017
PHP drop-down list - Part 2 (Serialization of elements to HTML)February 14, 2017