PHP Chapter 4
PHP Chapter 4
Forms
Unit - IV
• HTML controls like textbox, textarea, check boxes, radio buttons etc
enclosed in HTML form are used to collect data from user.
• When the user fills out the form and clicks submit button, the form
data is sent for processing to a PHP file.
• There are two methods “get” and “post” commonly used to send data
from HTML controls to PHP script on server.
• Standard Button
• Submit Button
• Reset Button Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
3
MHSSP
4.1 GUI Components
• When Get method is used, the submitted form data will be visible in the web
page address bar.
• It appends form data into the URL in name/value pair separated by &.
• The POST method does not display the submitted form data in the page
address field.
• POST has no size limitations, and can be used to send large amounts of data.
• Hidden controls are used to store the data in a webpage that user cant see.
• Hidden controls will be included in <form> element of a web page what will be
used to store data that will not be visible to the user, which will be sent to PHP
script available on the server.
• Data on the server will be fetched by any one of the method (GET or POST).
<?php
if(isset($_POST["userid"]))
{
echo "User ID: ".$_POST["userid"];
}
?>
• empty( ):
• It ensures that text field is not blank.
• This function accepts a variable as an argument and returns TRUE when the
text field is submitted with empty string, zero, NULL or FALSE value.
• is_numeric( ):
• It ensures that data entered in a text field is a numeric value.
• Function accepts a variable as an argument and returns TRUE when the text
field is submitted with numeric value.
• [abc] : find any character between the bracket. i.e. between a, b and c.
• \d : Find a digit.
• 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.
• Default is FALSE.
• It is an optional attribute.
• It is an optional attribute.
• If set to "/", the cookie will be available within the entire domain.
If set to "/php/", the cookie will only be available within the php
directory and all sub-directories of php.
• The default value is the current directory that the cookie is being
set in.
• It is an optional attribute.
• If set to TRUE the cookie will be accessible only through the HTTP
protocol (the cookie will not be accessible by scripting languages).
• This setting can help to reduce identity theft through XSS (Cross-
site Scripting) attacks.
• Default is FALSE.
• Default is 0.
• It is an optional attribute.
Syntax:
setcookie(name, value, expire, path, domain, secure, httponly);
• By default session variables last until the user closes the browser.
• Session variables hold information about one single user, and are
available to all pages in one application.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
44
MHSSP
4.6 Session
• Start of Session:
• Session variables are set with the PHP global variable: $_SESSION
Syntax:
mail(to,subject,message,headers,parameters);
Where:
To : Required. Specifies the receiver/s of the email.
Subject: Required. Specifies the subject of the email. It cannot contain any
newline characters.
Message: Required. Defines the message to be sent. Each line should be
separated with (\n). Line should not exceed 70 characters.
Headers: Optional. Specifies additional headers, Cc and Bcc.
Parameters: Optional. Specifies an additional parameter to the sendmail
program.
Prepared By: Khan Mohammed Zaid, Lecturer, Comp. Engg.,
48
MHSSP