Getting user input
Communicating with a server
Dr Graeme StuartCommunicating with a server
Dr Graeme StuartRecall that all communication between the client and the server happens via HTTP. The server includes the requested document in the response.
In a normal scenario, we load a page using a
GET
request, e.g. with a link.
|
|
It is also possible to load a page by submitting a form.
|
|
We can also use the fetch API in JavaScript to make HTTP requests but this is out of scope for this module.
<input>
elements are inline and void (i.e. they have no content and don’t need closing tags).
The <input>
element is extremely versatile, attributes are used to change its behaviour.
The default
<input>
is a text input like this:
|
|
Adding placeholder text is easy:
|
|
Default values can also be provided:
|
|
We will only cover some of the basics.
For accessibility,
<input>
elements should have an associated<label>
element.
|
|
|
|
In some use cases, you may not want/need a visible
<label>
. In this case, you can use anaria-label
attribute.
By default
<input>
elements are generic text fields but they can be modified with thetype
attribute.
|
|
|
|
|
|
There are many input field types available. Some of these were introduced with HTML5.
Getting dates and times.
|
|
Make sure you use these where possible.
|
|
|
|
We can use
<fieldset>
elements to organise<inputs>
into groups.
Select boxes need nested options.
|
|
Only one option can be
selected
Radio buttons need to be grouped using the
name
attribute.
|
|
The
value
attribute will be submitted as the value of thename
key.
Validation used to be really annoying, now its easy. Inputs with
type="tel"
,type="url"
andtype="email"
provide basic checking which is detected by the pseudo-classes:valid
and:invalid
. We can also add therequired
attribute to activate the:invalid
state on empty values.
|
|
Forms will only submit when all inputs are valid. They also have the same pseudo-classes so can also be styled.
Given input elements in the page, you will often want to access the user input via javascript.
|
|
|
|
User input can be easily accessed and set with JavaScript.
For most cases you will only need to access the
value
property or thechecked
property.
Two important events are the
input
andchange
events.
|
|
The input
event is triggered when user interacts with an input element.
|
|
The change
event is triggered when focus moves away and a new value is set.
|
|
Using JavaScript is entirely optional in this module.
Submitting forms passes data to the server either via GET
requests or POST
requests.
The difference is important.
GET
requests can includequery parameters
orsearch parameters
as part of the URL. These become visible to the end user and so should not be used for sensitive data.
Either way, when the server receives this request, it can parse the url to extract the parameters.
In PHP this is done automatically and the data are stored as associative arrays known as
$_GET
and$_POST
.
For example, I can link to a specific slide in the presentation by adding search parameters to the url of a link.
If you have any questions, now is a good time to ask.
Thanks for listening
Dr Graeme Stuart