Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

DCIT111 – Advanced Programming hayperaktib

Lecture # 7

Introduction to Form Variables


PHP was designed as a Web-based programming language to create dynamic Web content, to gather
and manipulate information submitted by HTML forms. For now, because we are talking about
variables, we will examine a simple form and how PHP collects and stores the form information in
variables.

$_REQUEST
- a superglobal array that contains the information submitted to the server from the HTML form.
- after extracting this information, PHP will create simple variables corresponding to the form data
as shown in Figure 2.

$_GET
- a superglobal array that contains the information submitted to the server from the HTML form
through get method.

$_POST
- a superglobal array that contains the information submitted to the server from the HTML form
through post method.

How PHP Handles Form Input


For each HTML form parameter, PHP creates a global variable by the same name and makes it
available to your script. For example, consider this HTML input type for two text fields:

<input type=”text” name=”boxer_name”>


<input type=”text” name=”boxer_division”>

Note:
 if you have a textfield named “boxer_name”, PHP create a variable called $boxer_name and if
you have another textfield named “boxer_division”, PHP will create a variable called
$boxer_division.
 the values assigned to the PHP variables are the same values the user entered in the HTML text
fields when filling out the form.

Figure 1 and Figure 2 illustrates a simple HTML form consisting of two fields. The form starts
with the opening <form> tag.

ACTION attribute
- assigned the name of the PHP script that will handle the form input: <form ACTION=”php
script”>.
- the browser knows where to send the data

After the user fills out the form and presses the submit button, the values that he or she typed
in the text boxes will sent to the PHP script.

METHOD attribute
- how to send the data based on the ACTION attribute of the <form> tag.

2 HTTP Methods
It is used to send the form data to the server.
 GET method
- this is the default value so you don’t have to explicitly assign it as an attribute.
- tells the browser to send a URL-encoded string, called the query string, to the server
- attaches the encoded query string to the end of the URL in the browser’s location box,
prepended with a ?.

1
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
- a method used when doing searches or handling static pages and GET query string are
limited in size.
- amount of informations to be send must not exceed to 100 characters.
 POST method
- the METHOD attribute must be added to the HTML <form> tag.
- The browser sends an encoded message body in the HTTP header to the server so that it
doesn’t appear in the URL.

query string

http://localhost/phpexamples/form1.php?boxer_name=Manny+Pacquiao&boxer_division=Welterweight

browser server

http://localhost/.../form1.htmll

Enter boxer name: GET METHOD


Manny Pacquiao
HTTP PHP
Enter boxer division:
Welterweight $boxer_name=” Manny Pacquiao”
$boxer_division=”Welterweight”
submit

Figure 1. The GET method sends form input in the URL.

http header

boxer_name=Manny+Pacquiao&boxer_division=Welterweight

browser server

http://localhost/.../form1.html

Enter boxer name:


POST METHOD
Manny Pacquiao PHP
HTTP
Enter boxer division:
Welterweight $boxer_name=”Manny Pacquiao”
$boxer_division=”Welterweight”
submit

Figure 2. The POST method sends form input in the HTTP header.

2
DCIT111 – Advanced Programming hayperaktib
Lecture # 7

3
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
Example 1.

<html>
<body>
<form action="form1act.php">
Enter boxer name: <br>
<input type="text" size=30 name="boxer_name">
Enter boxer division: <br>
<input type="text" size=30 name="boxer_division">
<br>
<input type="submit" value="submit">
</body>
</html>
Explanation:
1. The HTML <form> tag starts the form. The URL of the script that will handle the form data is
assigned to the action attribute. The “method” on how the data will be transmitted is
assigned to the method attribute. Because the GET method is the default method for
transmitting data to the server, you do not have to explicitly assign it as an attribute.
2.,3. The HTML input type is a text box that is set to hold 30 characters. One is named
“boxer_name” and other is named “boxer_division”.
4. After the user enters his or her name and phone number in the respective text boxes, and
presses the submit button, the browser will collect and URL encode the input data, then
send it to the server. The server will hand it to the PHP script listed in the action attribute
on line 1. When PHP gets the input data, it will decode it, and create a variable called
$boxer_name and a variable called $boxer_division and give it the values that were
entered in the form by the user.
5. This </form> tag ends the HTML form.

Example 2.

(The PHP Script form1act.php)

<?php
extract($_REQUEST)
print “The boxer name is $boxer_name. <br>”;
print “The boxer division is $boxer_division.”;
?>

or in the HTML document use the PHP shortcut tags:

<HTML><head><title>Testing Variables</title></head>
<body>
The boxer name is <?=$boxer_name?> and the boxer division is <?
=$boxer_division?>
</body></HTML>

Explanation:
1. The browser bundles up the input data, encodes it, and attaches it as a query string to the
URL as:
http://localhost/webprog/form1act.php?boxer_name=Manny+Pacquiao&boxer_division=Welterweight
PHP decodes the query string; that is, it removes the + signs and & and any other
encoding characters, and then creates global variables, called $boxer_name and $boxer_division, and
assigns values based on the user input. In this example, the values of the variables are
printed as part of the script.

4
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
2. You can also use the shortcut tags within the HTML document to display the value of the
PHP variables.

Predefined Variables
PHP provides a number of predefined variables, some that are not fully documented because
they depend on which server is running, its configuration, and so on. Some are defined in the php.ini
file. These variables describe the environment, server, browser, version number, configuration file, and
so on.

Table 1. Predefined Variables

Variable What It Does

AUTH_TYPE If running the Apache server as a module, this is set to the


authentication type.

DOCUMENT_ROOT The full path of the Web’s document root, normally where HTML
pages are stored and defined in the server’s configuration file.

HTTP_USER_AGENT Identifies the type of Web browser to the server when it requests
a file.

HTTP_REFERER The full URL of the page that contained the link to this page. Of
course if there isn’t a referring page, this variables would not
exist.

REMOTE ADDRESS The remote IP address of the client machine that requested the
page.

There are more predefined variables; which ones are set depends on your PHP configuration.

phpinfo() – can be used to retrieve built-in variables that have been set.

<?php
phpinfo(INFO_VARIABLES);
?>

String Functions
The strlen() function

The strlen() function is used to find the length of a string.

Let's find the length of our string "Hello world!":

<?php
print strlen("Hello world!");
?>

The output of the code above will be:

12

5
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
The strpos() function

The strpos() function is used to search for a string or character within a string.

If a match is found in the string, this function will return the position of the first match. If no match is
found, it will return FALSE.

Let's see if we can find the string "world" in our string:

<?php
print strpos("Hello world!","world");
?>

The output of the code above will be:

As you see the position of the string "world" in our string is position 6. The reason that it is 6, and not
7, is that the first position in the string is 0, and not 1.

Operators are used to operate on values.


PHP Operators
This section lists the different operators used in PHP.

Arithmetic Operators
Operator Description Example Result
+ Addition x=2 4
x+2
- Subtraction x=2 3
5-x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
% Modulus (division remainder) 5%2 1
10%8 2
10%2 0
++ Increment x=5 x=6
x++
-- Decrement x=5 x=4
x--

Assignment Operators

Operator Example Is The Same As


= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y

6
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
Comparison Operators

Operator Description Example


== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Logical Operators

Operator Description Example


&& And x=6
y=3

(x < 10 && y > 1) returns true


|| Or x=6
y=3

(x==5 || y==5) returns false


! Not x=6
y=3

!(x==y) returns true

Conditional statements are used to perform different actions based on different conditions.

Conditional Statements

Very often when you write code, you want to perform different actions for different decisions.

You can use conditional statements in your code to do this.

In PHP we have the following conditional statements:

 if statement - use this statement to execute some code only if a specified condition is true
 if...else statement - use this statement to execute some code if a condition is true and another
code if the condition is false
 if...elseif....else statement - use this statement to select one of several blocks of code to be
executed
 switch statement - use this statement to select one of many blocks of code to be executed

The if Statement

Use the if statement to execute some code only if a specified condition is true.

Syntax
if (condition) code to be executed if condition is true;

7
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
The following example will output "Have a nice weekend!" if the current day is Friday:

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri") echo "Have a nice weekend!";
?>
</body>
</html>

The if...else Statement

Use the if....else statement to execute some code if a condition is true and another code if a condition
is false.

Syntax
if (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;

Example

The following example will output "Have a nice weekend!" if the current day is Friday, otherwise it will
output "Have a nice day!":

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>
</body>
</html>

If more than one line should be executed if a condition is true/false, the lines should be enclosed within
curly braces:

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  {
  echo "Hello!<br />";
  echo "Have a nice weekend!";
  echo "See you on Monday!";

8
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
  }
?>
</body>
</html>

The if...elseif....else Statement

Use the if....elseif...else statement to select one of several blocks of code to be executed.

Syntax
if (condition)
  code to be executed if condition is true;
elseif (condition)
  code to be executed if condition is true;
else
  code to be executed if condition is false;

Example

The following example will output "Have a nice weekend!" if the current day is Friday, and "Have a nice
Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":

<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
  echo "Have a nice weekend!";
elseif ($d=="Sun")
  echo "Have a nice Sunday!";
else
  echo "Have a nice day!";
?>

</body>
</html>

The PHP Switch Statement

Use the switch statement to select one of many blocks of code to be executed.

Syntax
switch (n)
{
case label1:
  code to be executed if n=label1;
  break;
case label2:
  code to be executed if n=label2;

9
DCIT111 – Advanced Programming hayperaktib
Lecture # 7
  break;
default:
  code to be executed if n is different from both label1 and label2;
}

This is how it works:


 A single expression (most often a variable) is evaluated once
 The value of the expression is compared with the values for each case in the structure
 If there is a match, the code associated with that case is executed
 After a code is executed, break is used to stop the code from running into the next case
 The default statement is used if none of the cases are true

Example
<html>
<body>
<?php
switch ($x)
{
case 1:
  echo "Number 1";
  break;
case 2:
  echo "Number 2";
  break;
case 3:
  echo "Number 3";
  break;
default:
  echo "No number between 1 and 3";
}
?>

</body>
</html>

10

You might also like