Unit 4
Unit 4
Sr No Type Control
1 Text Textbox
1. Using Textbox: User can enter text or single line using textbox
2. Using Password: User can enter confidential information such as password, bank acountno etc.
Example:
<form>
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car"> I have a car
</form>
Example
<form>
<input type="radio" name="gender" value="male">Male<br>
<input type="radio" name="gender" value="female">Female
</form>
<form>
Select your known languages:
<select name=”languages”>
<option selected>Hindi</option>
<option>Gujarati</option>
<option>English</option>
<option>Sanskrit</option>
</select>
</form>
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>
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”>>
7. 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>
8. 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>
<html>
<body>
<form>
<h2>Login Form</h2><br>
User Name: <input type="text" size="20" ><br>
Password: <input type="text" size="20"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
Output
</html>
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
The “form2.php” file looks like this:
<?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.
<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 the POST method packages the name/value pair
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 the It is secure because submitted data are passed
URL are visible in address field of browser. through HTTP handler.
It can’t be used to send binary data, like images or Using POST method can be used to send
word documents, to server. ASCII as well as binary data.
The data send by POST method can be
accessed using $_POST superglobal variable
4.3. Form Validation using PHP
While designing the form that accepts input from user, it is required to test input values entered bu the user
such as:
Whether it is in required format or not
Whether value entered or left by user
This process is known as Validation.
Regular Expression: It is a pattern that is used to match various text strings.
It is used to validate user input to determine whether it is entered in predefined format or not.
Using Regular Expression, you can validate numbers, email address, date, contact no etc.
Regular Expression use ereg() function or eregi() function to validate user input.
ereg() function is case sensitive and eregi() function is case insensitive.
Syntax for ereg() function:
ereg($pattern,$string)
where, $pattern specifies the pattern to be match
$string specifies the string to be matched with pattern.
<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:
</body>
</html>
Output:
Run a.php
64
4.5. Session: creating a session, storing and accessing session data and destroying session
Definition:A session is a way to store information (in variables) to be used across multiple pages.
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.
OUTPUT:
Welcome:abc
● Destroy a session:
A php session is destroyed by session_destroy() fucntion. This function does not need any argument
and a single call can destroy all session variables. You can also use unset() function to unset session
variables.
Example:
<?php
Prepared by: Department of Computer Engineering Page 11
Subject Name: Introduction to Web Development Unit No:4 Subject Code:4340704
unset($_SESSION[‘counter’]);
?>
OR
<?php
session_destroy();
?>
4.6. Cookies: setting a cookies, accessing cookies data and destroying cookies
● Cookies are text files stored on the client computer and they are kept of user tracking purpose. PHP
transparently supports HTTP cookies.
● 3 steps involved in identifying returning users:
⮚ Server script sends a set of cookies to the browser. Ex: name, age
⮚ Browser stored this information on local machine for future use.
⮚ When next time browser sends any request to web server then it sends those cookies
information to the server and server uses that information to identify the user.
● setcookie() function is used to set cookie. This function requires upto six arguments and called before
<html> tag.
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 1 stjan 1970. If this parameter will not set
then cookie will automatically expire.
Path: Specify directories for which cookie is valid.
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 echo “set cookies”; ?></body></html>
OUTPUT
OUTPUT:
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: