PHP Unit 4 Updated (1) - 1
PHP Unit 4 Updated (1) - 1
• A web page having multiple forms: A web page having multiple forms can be processed in two
types:
1. Multiple functionalities can be provided on a single web page by providing multiple forms on a
web page having different functionality.
2. Each form on this web page will be given a separate name that will uniquely identify the form
on a web page with multiple forms.
3. Data from each form should be given to separate PHP script file for processing by specifying
PHP script filename in the action attribute of the forms.
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:
• A web page having multiple forms: A web page having multiple forms can be processed in two
types:
4. Each PHP Script should be written in such a fashion that will handle all the data coming from
that form.
5. A disadvantage of this method is that we have to write a separate PHP script for each form,
which creates extra files for handing
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:( code with
output)
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms: (code with
output)
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:
⮚ 2.Posting all form to single PHP script file for processing
1. Multiple functionalities can be provided on a single web page by providing multiple forms on a web page
having different functionality.
2. Each form on this web page will be given a separate name that will uniquely identify the form on a web
page with multiple forms.
3. Data from each form should be given to a single PHP script file for processing by specifying PHP script
filename in the action attribute of the forms.
4. Each PHP Script should be written in such a fashion that it will handle all the data coming from multiple
forms.
5. Data from multiple forms can be identified by it submit button and the processing each form will be written
with help of if, else, and else if conditional statements.
6. The advantage of this method is that we have to write a single PHP script for processing of all forms,
which saves time in the creation and handling of extra files.
Unit IV: Creating and Validating Forms
⮚ Working with multiple forms:
⮚ 2.Posting all form to single PHP script file for
processing
A Form having Multiple Submit
Buttons
● Multiple operations can be provided on a single form by providing
a different buttons for different operation.
● Based on which button is clicked, data in the form is processed
differently for the operations mentioned on that button.
● Identification of the button is done by its name on the server and
corresponding operation is called with the help of if,else and else
if conditional statements.
Example: multibutton.html
<html>
<head>
<title> Multiple form Demo</title>
</head>
<body>
<form name= “mailform” methode= “post” action= “filename.php”>
?>
4.5 Cookies
What is a Cookie?
PHP Cookies is a small piece of information which is stored at client browser.it is used to
recognize the user.
Cookies is created at server side and saved to client browser.
Each time when client sends request to the server, cookie is embedded with request.
Such way, cookie can be received at the server side.
In short cookie can be created,send and received at server end.
A cookie is often used to identify a user. A cookie is a small file that the server embeds
on the user's computer. Each time the same computer requests a page with a browser, it
will send the cookie too. With PHP, you can both create and retrieve cookie values.
Create Cookies With PHP
A cookie is created with the setcookie() function.
Syntax
setcookie(name, value, expire, path, domain, secure,
httponly);
PHP Create/Retrieve a Cookie
The following example creates a cookie named "user" with the value "John Doe".
The cookie will expire after 30 days (86400 * 30). The "/" means that the cookie
is available in entire website (otherwise, select the directory you prefer).
We then retrieve the value of the cookie "user" (using the global variable
$_COOKIE). We also use the isset() function to find out if the cookie is set:
Example
Modify a Cookie Value
To modify a cookie, just set (again) the cookie using the setcookie() function:
Example
Delete a Cookie
To delete a cookie, use the setcookie() function with an expiration date in the
past:
Example
PHP - $_SESSION
One of the superglobal variables in PHP, $_SESSION is an associative array of session
variables available in the current script. $HTTP_SESSION_VARS also contains the
same information, but it is not a superglobal.
What is a Session?
● Session are used to store important information such as the user ID more securely on the
server where malicious users cannot temper with them.
● To pass values from one page to another.
● Sessions are the alternative to cookies on browsers that do not support cookies.
● You want to store global variables in an efficient and more secure way compared to
passing them in the URL.
● You are developing an application such as a shopping cart that has to temporary store
information with a capacity larger than 4KB.
The session_start() Function
To read back the value of a session variable, you can use echo/print statements, or var_dump() or
print_r() functions.
echo $_SESSION[ "var"];
To obtain the list of all the session variables in the current session, you can use a foreach loop to
traverse the $_SESSION −
foreach ($_SESSION as $key=>$val)
echo $key . "=>" . $val;
To manually clear all the session data, there is session_destroy() function. A specific session variable
may also be released by calling the unset() function.
unset($_SESSION[ "var"]);
Example
The following PHP script renders an HTML form. The form data
is used to create three session variables. A hyperlink takes the
browser to another page, which reads back the session
variables.
Save this code as "test.php" in the document root folder, and
open it in a client browser. Enter the data and press the
Submit button.
When you click the "Submit" button, it will show a list of
all the session variables created −