Sunteți pe pagina 1din 20

PHP FORMS

WEB PROGRAMMING

MEMBERS:
VELASCO MAURICIO
BANAAG ARRIOLA
ARANETA SANTILLAN
FRANCISCO VALERIANO
AGUILAR DIVINA
CONTENTS
PHP FORMS

01 02 03 04 05
PHP FORMS FORM FORM FORM FORM
VALIDATION DIFFERENT REQUIRED COMPLETE
ELEMETS FIELDS

company name www.yourname.com


PHP FORMS

01
REPORTER:
PHP FORMS
PHP FORMS

GET AND POST METHOD

GET and POST create an array which holds key/value pairs, where keys are the name of the
form controls and the values are the input data user. Both GET and POST method are
treated as _GET and _POST in PHP. These methods are superglobals, which means that
they are always accessible, and they can be accessed using any function, class or files.

 The $_GET method is an associative array of variables passed to the current script via
the URL parameters.
 The $_POST method is an array of variables passed to the current script via the HTTP
POST method. In this method the information is transferred in a hidden manner.

company name www.yourname.com


PHP FORMS
PHP FORMS

GET AND POST METHOD

GET METHOD POST METHOD


.
Before sending any information in In POST method, before sending
GET method, it converts values/data any information to the server, it
into a query string in URL which converts client’s information into a
contains both page link and query string in URL.
encoded information separated by
the (?) character.

company name www.yourname.com


PHP FORMS
PHP FORMS

GET VS POST METHOD

Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3
=> value3, ...)). This array holds key/value pairs, where keys are the names of the
form controls and values are the input data from the user. Both GET and POST are
treated as $_GET and $_POST. These are superglobals, which means that they are
always accessible, regardless of scope - and you can access them from any function,
class or file without having to do anything special.

 $_GET is an array of variables passed to the current script via the URL parameters
 $_POST is an array of variables passed to the current script via the HTTP POST
method.

company name www.yourname.com


PHP FORMS
PHP FORMS

GET AND POST METHOD

WHEN TO USE GET WHEN TO USE POST


Information sent from a form with the Information sent from a form with the POST
GET method is visible to everyone (all method is invisible to others (all names/
variable names and values are displayed values are embedded within the body of the
in the URL). GET also has limits on the HTTP request) and has no limits on the
amount of information to send. The amount of information to send.
limitation is about 2000 characters. Moreover POST supports advanced
However, because the variables are functionality such as support for multi-part
displayed in the URL, it is possible to binary input while uploading files to server.
bookmark the page. This can be useful in However, because the variables are not
some cases. GET may be used for displayed in the URL, it is not possible to
sending non-sensitive data. bookmark the page.

company name www.yourname.com


FORM VALIDATION

02
REPORTER:
FORM VALIDATIONS
PHP FORMS

AN EXAMPLE FORM SHOWN IN FIGURE 6.1


CONSIST OF THE FOLLOWING ELEMENTS:
 Name (required field - must contain letters
and whitespaces)
 E-mail (required field - must contain valid
email address)
 Website (optional field - if present, must
contain valid website URL)
 Comments (optional field - a multi-line text
field)
 Gender (required field - must select a radio
button )

company name www.yourname.com


FORM DIFFERENT ELEMENTS

03
REPORTER:
FORM DIFFERENT
ELEMENTS
PHP FORMS

The Name, E-mail, Website are input elements. Input elements, in particular, used text
and submit values for its types attribute in order to create text fields and buttons. The
HTML code for these input elements are:

 Name: <input type="text" name="name">


 E-mail: <input type="text" name="email">
 Website: <input type="text" name="website">

company name www.yourname.com


FORM DIFFERENT
ELEMENTS
PHP FORMS

Radio button shows several options to the users from which the user may select one. Mostly, it
displays a circle next to each option, filling in the circle that is currently selected. Creating sets of
radio button in HTML is a matter of creating several input elements all with the type attribute of
“radio” and the identical name attribute. The Gender field is a radio button and the HTML code of
the form looks like this:
 Gender:
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="male">Male

The text area is typically a large text field with multiple rows. To create a text area is through text
area element. The textarea element has three attributes – name, rows, and cols attribute. The
HTML code of the form is:
 Comment: <textarea name="comment" rows="8" cols="80">Type your comments here...
</textarea>

company name www.yourname.com


FORM DIFFERENT
ELEMENTS
PHP FORMS

There’s another element called list, which offers options from which the user might choose.
A list can be created using the select element, within which is nested option elements for
each option to appear. The select element has a name attribute giving the name for the
browser to use when identifying the selection when the form is submitted. The option
element has a value attribute for specifying what value to send when that option is selected,
and it has a select attribute which allows the HTML to specify which option is initially
selected.
 The code follows:
Country: <select name="country">
<option value="php" selected="selected">Afghanistan</option>
<option value="albania">Albania</option>
<option value="algeria">Algeria</option>
</select>

company name www.yourname.com


FORM REQUIRED FIELDS

04
REPORTER:
FORM REQUIRED
FIELDS
PHP FORMS

Here is a simple PHP script that checks the name for empty input and throws an error
message if the input is empty:
if (empty($_POST["name"])) {
$nameErr = "Please enter your name";
} else {
$name = test_input($_POST["name"]);
}
In the code above, $nameErr variable is added, this variable for error will hold error
messages for the required fields. An if else statement is also added for each $_POST to
check if the variable is empty. If it is empty, an error message will be stored in the
$nameErr variable, and if it is not empty, it sends the user input data through the
test_input() function.

company name www.yourname.com


FORM REQUIRED
FIELDS
PHP FORMS

To display the error message in the HTML form (this will be generated if the user tries to
submit the form without filling in the required fields) use the code below:

Name: <input type="text" name="name">


<span class="error">* <?php echo $nameErr;?></span>

company name www.yourname.com


FORM COMPLETE

05
REPORTER:
FORM COMPLETE
PHP FORMS

To show the values in the input fields after the user hits the submit button, we add a little
PHP script inside the value attribute of the following input fields: name, email, and website.
In the comment textarea field, we put the script between the <textarea> and </textarea>
tags. The little script outputs the value of the $name, $email, $website, and $comment
variables.

Then, we also need to show which radio button that was checked. For this, we must manip
ulate the checked attribute (not the value attribute for radio buttons):

company name www.yourname.com


FORM COMPLETE
PHP FORMS

PHP COMPLETION
FORM EXAMPLE:
Here is the complete code for the
PHP Form Validation Example:

company name www.yourname.com


THANK YOU!
RESOURCES: STI ELMS

SUBMITTED TO:
MR. KENJI BELTRAN LAURENTE
WEB PROGRAMMIN-PROFESSOR

company name www.yourname.com

S-ar putea să vă placă și