Unit - 3
Unit - 3
There are two ways the browser client can send information to the web server.
The GET Method
The POST Method
Before the browser sends the information, it encodes it using a scheme called URL encoding.
In this scheme, name/value pairs are joined with equal signs and different pairs are separated by the ampersand.
name1=value1&name2=value2&name3=value3
Access submitted data in the relevant array for the submission type, using the
input name as a key.
<form action=“path/to/submit/page”
method=“get”>
<input type=“text” name=“email”>
</form>
$email = $_GET[‘email’];
FORMS: HOW THEY WORK
We need to know..
<form action=“path/to/submit/page”
method=“get”>
<!–- form contents -->
</form>
FORM TAGS
action=“…” is the page that the form should submit its data to.
method=“…” is the method by which the form data is submitted. The option are either get or
post. If the method is get the data is passed in the url string, if the method is post it is passed as a
separate file.
INPUT
INPUT attributes
type: the kind of user input control
name: the name of the control
This gets passed through to the handling code
In PHP: $_POST[‘name’]
value: initial value of the control
size: initial width of the control
in pixels, except for text and password controls
maxlength: for text/password, maximum number of characters allowed
checked: for radio/checkbox, specifies that button is on
src: for image types, specifies location of image used to decorate input button
PREPARED BY : MS. PREETI BHATT
SPECIAL BUTTONS
File upload
file: creates a file upload control
text:
password:
checkbox:
radio:
button
hidden:
Use a text input within form tags for a single line freeform text input.
name=“…” is the name of the field.You will use this name in PHP to access the data.
id=“…” is label reference string – this should be the same as that referenced in the
<label> tag.
size=“…” is the length of the displayed text box (number of characters).
FORM FIELDS: PASSWORD INPUT
<label for=“pw">Password</label>
<input type=“password"
name=“passwd"
id=“pw"
size="20"/>
FORM FIELDS: TEXT INPUT
<label for="desc">Description</label>
<textarea name=“description”
id=“desc“
rows=“10” cols=“30”>
Default text goes here…
</textarea>
FORM FIELDS: TEXT AREA
name=“…” is the name of the field.You will use this name in PHP to access the data.
id=“…” is label reference string – this should be the same as that referenced in the
<label> tag.
rows=“…” cols=“..” is the size of the displayed text box.
FORM FIELDS: DROP DOWN
name=“…” is the name of the field. All radio boxes with the same name are grouped
with only one selectable at a time.
id=“…” is label reference string.
value=“…” is the actual data sent back to PHP if the option is selected.
checked=“checked” this option is selected by default.
FORM FIELDS: CHECK BOXES
What colours do you like?<br />
<input type="checkbox"
name="colour[]"
id="r"
checked="checked"
value="red" />
<label for="r">Red</label>
<br />
<input type="checkbox"
name="colour[]"
id="b"
value="blue" />
<label for="b">Blue</label>
FORM FIELDS: CHECK BOXES
<input type="hidden"
name="hidden_value"
value="My Hidden Value" />
A submit button for the form can be created with the code:
<input type="submit"
name="submit"
value="Submit" />
FIELDSET
In XHTML 1.0, all inputs must be grouped within the form into fieldsets. These represent logical
divisions through larger forms. For short forms, all inputs are contained in a single fieldset.
<form>
<fieldset>
<input … />
<input … />
</fieldset>
<fieldset>
<input … />
<input … />
</fieldset>
</form>
A WARNING..
We also need to check before accessing data to see if the data is submitted, use isset() function.
if (isset($_POST[‘username’])) {
// perform validation
}
VALIDATION
filter_var(string , FILTER_VALIDATE_OPTION)
FILTER_VALIDATE_BOOLEAN
FILTER_VALIDATE_DOMAIN
FILTER_VALIDATE_EMAIL
FILTER_VALIDATE_FLOAT
FILTER_VALIDATE_INT
FILTER_VALIDATE_IP
FILTER_VALIDATE_MAC
FILTER_VALIDATE_REGEXP
FILTER_VALIDATE_URL