UNIT 4-1 of diploma
UNIT 4-1 of diploma
_______________________________________________________________________________________
Teaching hours :08 Total marks :14
Lecturer Name : B.P.Patel
_______________________________________________________________________________________
Form controls: Text Box, Textarea, List Box, Dropdown, Check Box, Radio Box, Buttons, Upload,
color, date etc.
Introduction to Forms:
➢ Whenever you are going to design a page, which can accept information from users through
various input fields.
➢ We can define a form in PHP page using <form>..</form> tag.
➢ A form can contain input elements like
1. Textfield
2. Textarea
3. Checkboxes
4. Radio-buttons
5. Drop-down list
6. Submit buttons etc..
➢ Forms are used to pass data to a server.
➢ The FORM element has no formatting attributes.
Input Elements: Input elements are used to get input from user in various format. Its properties are specified
in TYPE attribute of <INPUT> ..</INPUT> tag.
Input Elements Properties:
Sr No Type Control
1 Text Textbox
name or id It specifies the name of the form and is used to identify individual forms.
It specifies the location to which the form data has to be sent when the form is
action
submitted.
It specifies the HTTP method that is to be used when the form is submitted. The
method possible values are get and post. If get method is used, the form data are visible
to the users in the url. Default HTTP method is get.
encType It specifies the encryption type for the form data when the form is submitted.
novalidate It implies the server not to verify the form data when the form is submitted.
List Box: This is like List control of visual basic. Select list is used to select one or more option from
given list of choice.
<html>
<body>
<h2>LIST BOX</h2>
<form>
Select your known languages:
<select name=”select” size=”4” multiple=”true”>
<option></option>
<option selected>Hindi</option>
<option>Gujarati</option>
<option>English</option>
<option>Sanskrit</option>
</select>
</form>
</body></html>
Output:-
7. Button control:
Push Button: This element would be used to cause an action to take place.
Syntax: <input type=”button” name=”button” value=”button”>
TYPE: Indicates type of input element. NAME:
Variable name passed to application. VALUE:
Data associated with variable name
Submit Button: Every set of form tags requires a Submit button. It causes the browser to send the
names and values of other elements to application specified by ACTION attribute of Form element.
Syntax: <input type=”submit” name=”submit” value=”submit”>
TYPE: Indicates type of input element. NAME:
Variable name passed to application. VALUE:
Data associated with variable name
Image Submit Button:Allows you to substitute an image for the standard submit button.
Syntax: <input type=”image” src=”url” name=”image”>
Reset Button:It is good idea to include one of these for each form where user are entering data. It allows
user to clear all input in form.
Syntax: <input type=”reset” name=”reset” value=”reset”>>
8. color : It defines a color picker. The default value is #000000 (black).
<form >
Select your favorite color:<input type="color" name="favcolor" value="#ff0000"><br>
<input type="submit">
</form>
Output:-
9. Date : It defines a date picker. The resulting value includes the year, month, and day
<form>
Birthday:<input type="date" name="birthday"><br>
<input type="submit">
</form>
Output:-
● ACTION: It is the URL of the CGI (Common Gateway Interface) program that is going to accept
data from the form, Process it, then Send a response back to browser.
● METHOD:GET(default) or POST specifies which HTTP method will be used to send the form’s
contents to web server.
● ENCTYPE: Mechanism used to encode the form contents. You can leave this attribute as default.
● NAME: This attribute specifies the name of the Input control.
● TARGET: Target frame where response page will show up
GET Method:
● The GET method passes arguments from in page to next page as a part of the URL Query String.
● When used for form handling, GET appends the indicated variable name and value to the
URL designated in the ACTION attribute with a question mark separator.
● Each item submitted via GET method is accessed in the handler via $_GET array.
Example: form1.php
<html>
<body>
<form action="form2.php" method="GET">
Enter Your Name: <input type="text"name="name"><br> Enter Your City: <input type="text" name="city"
><br>
<input type="submit" value="OK" >
<input type="reset" value="Cancel" >
</form>
</body>
</html>
URL: http://localhost/ami/form2.php?name=abc&city=ahmedabad
“form2.php”
<?php
$Name=$_GET['name'];
$City=$_GET['city'];
echo "Your Name is :". $Name."<br>";
echo "Your City is :". $City."<br>";
?>
Output:
POST Method:
➢ POST method is the preferred method of form submission.
➢ The form data set is included in the body of the form when it is forwarded to the processing
agent (web server).
➢ No visible change to the URL will result according to the different data submitted.
➢ Each item submitted via POST method is accessed in the
handler via the $_POST array.
Advantages of POST method:
➢ It is more secure then GET because user entered information is never visible in URL.
➢ There is much larger limit on the amount of data that can be passed (a couple of kilobytes).
Disadvantages of POST method:
➢ The result at a given moment cannot be book marked.
➢ The result should be expired by the browser, so that an error will result if the user
employs the Back button to revisit the page.
➢ This method can be incompatible with certain firewall setups
form1.php
<html>
<body>
<form action="form2.php" method="POST">
Enter Your Name: <input type="text"name="name"><br> Enter Your City: <input
type="text" name="city" ><br>
<input type="submit" value="OK" >
<input type="reset" value="Cancel" >
</form>
</body>
</html>
URL: http://localhost/form2.php
<?php
$Name=$_POST['name'];
$City=$_POST['city'];
echo "Your Name is :". $Name."<br>";
echo "Your City is :". $City."<br>";
?>
Output:-
Using GET method data is sent from one page to Using POST method data is sent from one
other in the URL page to other within the body of the HTTP
request
The GET method, appends name/value pairs to POST method packages the name/value pair
the URL inside body of HTTP request, which makes
for a clean URL
The length of URL is limited, so it works if there POST method imposes no size limitations on
are few parameters. forms output.
It is insecure because parameters passed on It is secure because submitted data are passed
the URL are visible in address field of browser. through HTTP handler.
It can’t be used to send binary data, like images Using POST method can be used to send
or word documents, to server. ASCII as well as binary data.
<html>
<head></head>
<body>
<form method="POST" >
Name:<input type="text" name="txtName"><br> ContactNo:<input type="text" name="txtNo"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
<?php
if (isset($_POST['submit']))
{
$Name=$_POST['txtName'];
$ContactNo=$_POST['txtNo'];
if(empty($Name))
{
echo "Please Enter Name";
}
if(empty($ContactNo))
{
echo "Please Enter Contact No";
}
if(!preg_match("/[0-9]{10}/",$ContactNo))
{
echo "<br>Please Enter Proper Mobile No";
}
}
?>
Output:
b.php
<?php
if ($_POST['add'])
{
echo $_POST['number']+10;
}
else if ($_POST['subtract'])
{
echo $_POST['number']-10;
}
?>
Output:-
64
When you work with an application, you open it, do some changes, and then you close it. This is
much like a Session. The computer knows who you are. It knows when you start the application and
when you end. But on the internet there is one problem: the web server does not know who you
are or what you do, because the HTTP address doesn't maintain state.
➢ Cookies are text files stored on the client computer and they are kept of user tracking purpose.
PHP transparently supports HTTP cookies.
Syntax: setcookie(name,value,expire,path,domain,security);
Name: set the name of cookie and stored in environment variable called HTTP_COOKIE_VARS.
Value: set the value of named variable and content that you want to store.
Expiry: Specify a future time in seconds since 00:00:00 GMT on 1stjan 1970. If this parameter will not set
then cookie will automatically expire.
Domain: Specify domain name in very large domains and must contain at least two periods to be valid. All
cookies are only valid for the host and domain which created them.
Security: if set to 1 then specify that cookie sent by secure transmission using HTTPS otherwise set to
0 means cookie sent by regular HTTP.
Example: create 2 cookies name and age and these coolies will expire after one hour.
Code: setcookie.php
<?php
setcookie(“name”,”abc”, time()+3600); setcookie(“age”, “25”, time()+3600, “/”, “”,0);
?>
<html>
<head>
<title></title></head>
<body>
<?php
?>
</body>
</html>
Output:-
set cookies
➢ Example:
Code: method1.php
<html>
<head>
<title>Accessing cookies with php</title>
</head>
<body>
<?php
</body></html>
Output:-
ABC
25
Check cookie is set or not with php:
Example:
Code: cookie.php
<html><head><title></title></head>
<body>
<?php if(isset($_COOKIE[“name”]))
echo “Welcome”.$_COOKIE[“name”] . “<br/>”;
else
echo “Sorry cookie not recognized <br/>”; ?>
</body></html>
OUTPUT:
Welcome ABC
Delete cookiewith php:
To delete a cookie call setcookie() function with name argument only this does not always work well. So it is
safest to set the cookie with a date that has already expired:
Example:
Code: deletecookie.php
<?php
setcookie(“name”,””,time()-60);
setcookie(“age”,””,time()-60);
?>
<html><head><title>Deleteingcookites</title></head>
<body>
<?php
echo “Cookies deleted”;
?>
</body>
</html>
OUTPUT:
Difference between Cookie And session
Cookies Session
It can only store a certain amount of info. It can hold an indefinite quantity of data.
Cookies Session
Because cookies are kept on the local computer, To begin the session, we must use the session start()
we don’t need to run a function to start them. method.
Cookies are not secured. Session are more secured compare than cookies.
Cookies stored data in text file. Session save data in encrypted form.
In PHP, to get the data from Cookies , In PHP , to get the data from Session, $_SESSION
$_COOKIES the global variable is used the global variable is used