0% found this document useful (0 votes)
7 views5 pages

PHP Form Exercises and Development Guide

This document describes several exercises on form development in PHP. It explains how to create forms, retrieve and process submitted data, manage errors, and validate inputs. Several examples are provided, such as a calculator, a guessing game, or a contact form.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

PHP Form Exercises and Development Guide

This document describes several exercises on form development in PHP. It explains how to create forms, retrieve and process submitted data, manage errors, and validate inputs. Several examples are provided, such as a calculator, a guessing game, or a contact form.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ISET Sousse / Server-side Development Lotfi Khediri

Exercise N°4: Forms in PHP


Exercise 1: To start
Develop a page that displays the following form:

By clicking the 'Send Price' button, a page appears containing:

Price in dinars: ……

Price in euros: ....

Exercise 2: Form and GET method


1. Write a form [Link] (see figure 2.1(a)) in which the user
can enter the value of the multiplier before calling the script
[Link](cf. figure 2.1(b)).

2. Write a form [Link] (see figure 2.2(a)) in which the user


can enter the value of the multiplicand (maximum) and the multiplier
maximum, and can choose the display mode of the table
multiplication :
from 0 to m times the number nb (as in [Link]) (see figure
2.2(b)) ;
from 0 to m times the numbers from 0 to nb (as in [Link]) (cf.
figure 2.2(c)).
The new script called here, [Link], must test the chosen mode and carry out
the proper display.

1
ISET Sousse / Server-side Development Lotfi Khediri

3. What happens if the user forgets to enter the multiplicand and/or the
multiplier, or make incorrect entries in these fields of
form? Complete the processing script ([Link]) to make it complete.
robust to these problems.
[Link] alternative is to execute the processing only when the form has
has been properly filled out. For this, the form and its processing must
to be grouped in a single PHP script. Write this script ([Link]), which
doit être redirigé sur lui-même (attributactionde la balise<form>), sauf
when all the information is correct (it then executes the processing
- here, the display of the multiplication table).

Exercise 3: Parameters in the URL

We define the following table:

$personnes = array(
'mdupond'=> array('prenom' => 'Martin', 'nom' => 'Dupond', 'age' => 25, 'ville'
Paris
'jm'=> array('prenom' => 'Jean', 'nom' => 'Martin', 'age' => 20, 'ville' =>
Villetaneuse
'toto'=> array('prenom' => 'Tom', 'nom' => 'Tonge', 'age' => 18, 'ville' =>
Epinay
'arn'=> array('prenom' => 'Arnaud', 'nom' => 'Dupond', 'age' => 33, 'ville' =>
Paris
'email'=> array('prenom'=>'Emilie', 'nom'=>'Ailta', 'age'=>46,
'ville'=>'Villetaneuse'),
'dask' => array('prenom'=>'Damien',
'nom'=>'Askier','age'=>7,'ville'=>'Villetaneuse')
);
1. Call a PHP page by passing a parameter named 'namepseudo' in the URL.
for value one of the pseudonyms of the array $people. Ensure that the
page displays the username and the associated information contained in the table
$persons. Improve the script so that it displays Sorry, your
pseudonym does not appear in the list if the pseudonym is not a key of the
table$people.
2. Create a form allowing the user to enter the nickname to search for
in order to facilitate data entry for the user. Then directly put the form
in the previously created PHP script in order to be able to easily perform

2
ISET Sousse / Server-side Development Lotfi Khediri

multiple searches. Ensure that the username input field


contains the last entered value.
3. Create a php page '[Link]' that allows displaying all the usernames under
in the form of an unordered list (ul). Each username will be a link to a page
php "[Link]" which displays the information (name, surname, age, city) about this
pseudo.

4. On the previous page '[Link]', add a button (of type submit) that
Leads to a page '[Link]' that displays a form initialized by the
information about the questioned pseudonym. Write the page '[Link]'.

5. Create a second form asking for a username, a first name, a last name,
an age and a city, and adding a new person in the table$people
where the values are those given by the form. Add multiple people
and then explain the problem. (Display the entire table to better see the
problem.) How to remedy this (answer without code as the knowledge
necessary for the response exceed the scope of this chapter) ?

3
ISET Sousse / Server-side Development Lotfi Khediri

Exercise 4: Password protected HTML content

The purpose of this exercise is to create password-protected content. To do this,


choose a password (for example: kangaroo). Create a form allowing to
enter a password. The protected content should then only be displayed if the password is
The past is correct. If not, the form must be displayed again.

Exercise 5: Calculator
The purpose of this exercise is to create a simple calculator. The user must enter
two numbers and choose an operation among addition, subtraction,
multiplication or division. The result of the operation must then be displayed.

In order to facilitate user input, create a form containing only one


text type input tag where the user directly enters the operation (by
example: 17.5 * 13.46). Verify, using regular expressions, that the text
Input by the user corresponds to an operation. In this case, display the result.

Exercise 6: Contact Form

It is interesting that a user can leave a message for the webmaster or to


the administrator of a website. The idea is then to create a form where
The user enters their contact details as well as a message. When submitting the
form, a message is then automatically sent to the person
concerned. The purpose of this exercise is to create such a form and the processing
Associate PHP.

1. Create a form allowing a user to enter their username, number


of phone and email address. Verify, using regular expressions,
that the input verifies the following characteristics:

othe username consists of 4 to 8 letters,

othe phone number consists of 10 digits, the first being


a 0, the following a number between 1 and 6,

oThe email address contains a single at symbol, ends with '.fr', '.com'
or ".org", the other characters being letters, numbers or the
underscore symbol. In addition, the address must contain at least 3
characters before the at sign and between 4 and 10 characters between the at sign
and the end of the chain.

The PHP script must then indicate whether the input is correct and in that case,
display the entered information.

2. Improve the presentation of the web page using CSS.

4
ISET Sousse / Server-side Development Lotfi Khediri

Exercise 8: Guess the number game

The Guess the Number game is played by a single player. The computer chooses a number
random integer between 1 and 100 inclusive. The player has 7 attempts to
find this number. With each attempt, if the number entered by the user is
different from the random number, the program indicates if the entered number is higher
smaller or larger than the random number.

Program this game in PHP. To do this, create a form allowing


the user to choose a number. The number of attempts remaining will be
given using a hidden type input tag. The random number will be
also stored in a hidden type field.

You might also like