0% found this document useful (0 votes)
27 views

Unit Iv

The document discusses forms in PHP and how to create and validate them. It covers: 1) Forms are used to get input from users and submit it to servers for processing. Common form controls include text boxes, text areas, radio buttons, checkboxes, and lists. 2) Form data can be submitted via GET or POST methods. GET has limitations on data size while POST does not. PHP's $_GET and $_POST superglobals retrieve GET and POST data respectively. 3) Examples are given to demonstrate creating forms and retrieving/validating submitted data for various form elements and submission methods. Server-side validation using PHP is emphasized.

Uploaded by

sawantsankya4197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Unit Iv

The document discusses forms in PHP and how to create and validate them. It covers: 1) Forms are used to get input from users and submit it to servers for processing. Common form controls include text boxes, text areas, radio buttons, checkboxes, and lists. 2) Form data can be submitted via GET or POST methods. GET has limitations on data size while POST does not. PHP's $_GET and $_POST superglobals retrieve GET and POST data respectively. 3) Examples are given to demonstrate creating forms and retrieving/validating submitted data for various form elements and submission methods. Server-side validation using PHP is emphasized.

Uploaded by

sawantsankya4197
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

***UNIT-IV Creating and Validating Forms***

===========================================
***UNIT-IV Creating and Validating Forms***
===========================================
- PHP is becoming a standard in the world of web programming
becuase of its simplicity, performance, reliability, flexibility and
speed.
- Forms are very essential part of web development.
- Forms are used to get input from user and submit it to the web
server for processing.
- Forms are used to communicate between users and servers.
- How we process the form:
1) First, we will creates form where we can take input from users.
2) We will send form data to the server using GET and POST method.
3) At server side we will validate user and add data to the database
by using php code.

- We can define form using tag <form>....</form>..


- We can create and use forms in PHP.
- To get form data, we need to use PHP global variables $_GET and
$_POST.
- Form request may be get or post.

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

Browser Role GET and POST Methods:


==================================

- There are two http method that a client can use to pass form data
to the server i.e GET and POST methods.
- We use method attributes to specifiy method inside the form.

***GET Method:
- Using get method we can send data to the server as a part of URL.
- We can send limited data to the server using get method.
- Example:
http://www.msbte.com/login.html?username=value1&passwo
rd=value2
- When we send data to the server using get method then it will
produces long url string.
- Never use GET method if we have password or other sensitive
information to be sent to the server.
- The data send to the server, we can retrive by using php variable
$_GET.
- Example:
//Save below code in login.php file
<?php
if(isset($_GET['b1']))
{
echo "Your User Name:".$_GET['nm'];

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

echo "<br> Your Password:".$_GET['psw'];


}
?>
<html>
<body>
<form action="" method="GET">
Name:<input type="text" name="nm"></br><br>
Password:<input type="text" name="psw"><br><br>
<input type="submit" name="b1" value="Login">
</form>
</body>
</html>

***POST Method:
- Using POST method we can send data to the server as a part of
body.
- We can send large amount of data to the server using post method.
- POST method transfer data to the server as part of HTTP header.
- POST method does not have any restriction on data size to be sent
to the server.
- The data send to the server, we can retrive by using php variable
$_POST.
- Example:
//Save below code in login.php file

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

<?php
if(isset($_POST['b1']))
{
echo "Your User Name:".$_POST['nm'];
echo "<br> Your Password:".$_POST['psw'];
}
?>
<html>
<body>
<form action="" method="POST">
Name:<input type="text" name="nm"></br><br>
Password:<input type="text" name="psw"><br><br>
<input type="submit" name="b1" value="Login">
</form>
</body>
</html>

- isset function in PHP is used to determine whether a variable is set


or not.

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

Practice Program-1: Check Even or odd


<?php
if(isset($_GET['b1']))
{
if($_GET['tf1']!="")
{
if($_GET['tf1']%2==0)
{
echo "Number is EVEN";
}
else
{
echo "Number is ODD";
}
}
else
{
echo "Textfield should not be empty";
}
}
?>
<html>
<body>
<form method="GET">

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

Enter any number:<input type="text"


name="tf1"><br><br>
<input type="submit" name="b1" value="Check
EVEN/ODD">
</form>
</body>
</html>

Practice Program-1: Find out the factorial of given number


<?php
if(isset($_GET['b1']))
{
$no=$_GET['tf1'];
$i=1;
$fact=1;
while($i<=$no)
{
$fact=$fact*$i;
$i++;
}
echo "Factorial of Number=$fact";
}
?>
<html>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

<body>
<form method="GET">
Enter any Number:<input type="text"
name="tf1"><br><br>
<input type="submit" name="b1" value="Calculate
Factorial">
</form>
</body>
</html>

=================
Form Controls
=================
- There are different form controls that we can use to collect data
from user.
1) TextBox:
- This is form control which is used to enter single line text.
- It is also called as Text field, Text entry box which allow user to
enter single line text.
- Syntax:
<input type="text" name="nameoftextbox">
- if we use type as text then it will create textbox.
- Example:
<?php
if(isset($_GET['b1']))

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

{
echo "Welcome ".$_GET['tf1'];
}
?>
<html>
<body>
<form method="GET">
Enter Your Name: <input type="text" name="tf1">
<input type="submit" name="b1" value="Submit">
</form>
</body>
</html>

2) TextArea:
- This is multi-line textbox.
- This control allow user to enter multiple lines.
- Syntaxl:
<textarea name="Name" rows="count" cols="count">
</textarea>
- Example:
<?php
if(isset($_GET['b1']))
{
echo "Your Address: ".$_GET['tf1'];

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

}
?>
<html>
<body>
<form method="GET">
Enter Your Address: <textarea name="tf1" rows=10
cols=50></textarea>
<input type="submit" name="b1" value="Submit">
</form>
</body>
</html>

3) Radio Button:
- The radio button is used to select single choice from multiple
options.
- All radio buttons in the group have the same name attributes.
- Only one button can be selected per group.
- Radio button look like circle.
- Example:
<?php
if(isset($_GET['b1']))
{
$color_name=$_GET['c1'];
echo "You have selected color :$color_name";

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

}
?>
<html>
<body>
<form method="GET">
Please select your favourite Color:
<input type="radio" name="c1" value="white">White
<input type="radio" name="c1" value="red">Red
<input type="radio" name="c1" value="green">Green
<input type="radio" name="c1" value="gray">Gray
<input type="submit" name="b1" value="Submit">
</form>
</body>
</html>

4) Checkbox:
- A checkbox is a toggle button. It can be either ON or OFF.
- The value attributes should contain the value that will be sent to
the server.
- Checkbox consists of square box with associated label.
- Example:
<?php
if(isset($_GET['b1']))
{

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

if(!empty($_GET['c1']))
{
$Checked_count=count($_GET['c1']);
echo "You have selected ".$Checked_count."
options:<br>";
foreach($_GET['c1'] as $selected)
{
echo "<br>".$selected;
}
}
}
?>

<html>
<body>
<form method="GET">
<h3>Select your interested programming
language:</h3><br>
<input type="checkbox" name="c1[]" value="C Lang">C
Lang<br><br>
<input type="checkbox" name="c1[]" value="C++
Lang">C++ Lang<br><br>
<input type="checkbox" name="c1[]" value="JAVA
Lang">JAVA Lang<br><br>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

<input type="checkbox" name="c1[]" value="Python


Lang">Python Lang<br><br>

<input type="submit" name="b1" value="submit">


</form>
</body>
</html>

5) List:
- It will display list of items to a user.
- User can select an item from the list.
- User can select either one option from the list or multiple options.
- A multi-select lit allows the user to select multiple itens at once by
holding down CTRL key.
- Example:
<?php
if(isset($_GET['b1']))
{
foreach($_GET['transport'] as $x)
{
echo "You have selected item :$x <br>";
}
}
?>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

<html>
<body>
<form method="GET">

<select name="transport[]" multiple="multiple">


<option>BUS</option>
<option>CAR</option>
<option>RAILWAY</option>
<option>AIROPLANE</option>
</select>

<input type="submit" name="b1" value="submit">


</form>
</body>
</html>

6) Buttons:
- It will create push button.
- It is an interactive component which enable user to communicate
with an application.
- It is a standard button and it will generates event when we click on
it.
- Syntax:

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

<input type="button|submit|resent| name="nm"


value="vl">

Where:
button - The button is clickable button.
submit - The button is submit button whihc will submits
form data.
reset - The button is a reset button which will reset form
data its initial values.
- Example:
<html>
<body>
<form method="GET">
<input type="submit" name="b1" value="Submit">
<input type="button" name="b2" value="Button">
<input type="reset" name="b3" value="Reset">
</form>
</body>
</html>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

=====================
**WEB PAGE VALIDATION
=====================
- Validation means check the input submitted by the user.
- There are two types of validations available in PHP.
1) Client side validation: This validation performed on the client
machine web browsers.
2) Server side validation: After submitting data, the data has sent to
server and performed validation check in server machine.

- Some of the validation rules for fields:

Name - Shoudl required letters and white spaces.


Email - Should required @ symbol

Website - Should required a valid URL


Radio - Must be selectable at least once.
Checkbox - Must be selectable at least once.
Drop-Down Menu - Must be selectable at least once.

- Example:
<?php
if(isset($_GET['b1']))
{

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

if(empty($_GET['tf1']))
{
echo "Name is required!!!";
}
else
{
echo "Welcome ".$_GET['tf1'];
}
}
?>

<html>
<body>
<form method="GET">
Enter Your Name:<input type="text" name="tf1">
<input type="submit" name="b1" value="Submit">
</form>
</body>
</html>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

============================
**Web Page having many forms
============================
- In single web-page, we can create multiple forms.
- Example:
<?php
if(!empty($_POST['mailing-submit']))
{
echo "<h4>Form 1 is calling</h4>";
}
if(!empty($_POST['contact-submit']))
{
echo "<h4>Form 2 is calling</h4>";
}
?>

<html>
<body>
<form name="mailinglist" method="post">
<fieldset>
<legend>Form 1</legend>
<h4>Email:<input type="text" name="email"></h4>
<input type="submit" name="mailing-submit"
value="Add our mailing list">

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

</fieldset>
</form>

<form name="contacts" method="post">


<fieldset>
<legend>Form 2</legend>
<h4>Email:<input type="text" name="email"></h4>
<h4>Subject:<input type="text"
name="subject"></h4>
<h4>Body of Email:<textarea
name="msg"></textarea></h4>
<input type="submit" name="contact-submit"
value="Send Email">
</fieldset>
</form>
</body>
</html>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

====================================
Form having multiple submit buttons
====================================
- Here, we need to understand support multiple submit buttons
present in HTML then how PHP handle it.
- Normally a HTML form has only one submit button but there are
situtions when we might need to use more than one buttons and
PHP check which button has been preseed and an action to be done
according to the button pressed.
- Example:
<html>
<body>
<h3>Form having multiple submit buttons</h3>
<?php
switch($_REQUEST['b1'])
{
case "Button-1": echo "<h4>You pressed Button
1</h4>";break;
case "Button-2": echo "<h4>You pressed Button
2</h4>";break;
case "Button-3": echo "<h4>You pressed Button
3</h4>";break;
}
?>
<form method="POST">

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

<input type="submit" name="b1" class="button"


value="Button-1">
<input type="submit" name="b1" class="button"
value="Button-2">
<input type="submit" name="b1" class="button"
value="Button-3">
</form>
</body>
</html>

========================
***Cookies***
========================
- A cookie is a small piece of data in the form name-value pair.
- Cookies are sent by the web server and stored in the web browser.
- PHP cookie is small piece of information which is stored at client
browser in text format.
- It is used to recognize user.
- Cookies are also known web cookies.
- Cookie is created at server side and stored at browser side.
- Use setCookie() function to send the cookie to the web browser.
- This same function also used for delete the cookie.
- We use cookie for identify user.

- There are two types of cookie:

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

1) Session Cookie:
- Session cookie is also called as transient cookie.
- Session cookie erased when we close the web browser.
- This is also called as temporary cookie.
- Session cookie is stored in temporary memory and it got deleted
automatically when we close the browser.
- Session cookie do not collect information from the computer.

2) Persistent Cookie:
- Persistent cookie do not expire at the end of the session.
- Persistent cookie is also called as permanent cookie.
- A cookie that is stored on hard drive until it expires.
- We will collect information about the user from the persistent
cookie.

• isset() function:
- To read data from cookie, we first have to check if the cookie is
actually exists.
- This is achieved through the isset function.
- The isset() function is used to check existence of variable.
- Syntax:
isset($_COOKIE['nameofcookie']);
- If the cookie specified in the isset function is exists then function
will return true value otherwise it will return false value.

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

- Example:
• Attributes of Cookie:
- The following syntax shows attributes of cookies:

setCookie(name,value,expire,path,domain,secure,httponly);
- Only the name parameter is required, all other parameters are
optional.
- Where:
name - name of the cookie.
value - value of the cookie
expiry - time when cookie will get expire.
path - This specifies the directories for which the cookie is valid.
domain - The browser will return the cookie only for URLs
within this domain.The default is the server host name.
Secure - This can be set to 1 which specify that the cookie
should sent by using HTTPS otherwise set to 0 which means cookie
can be sent by using HTTP protocol.

• Create Cookie:
- Cookie can be created by using setCookie() function:
- Example:
<?php
if(isset($_GET['b1']))
{
$cookie_name=$_GET['tf1'];

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

$cookie_value=$_GET['tf2'];
setCookie($cookie_name,$cookie_value);
if(isset($_COOKIE[$cookie_name]))
{
echo "Cookie set successfully with below details:";
echo "<br>Cookie Name: $cookie_name";
echo "<br>Cookie Value: $cookie_value<br><br>";
}
else
{
echo "Cookie is not set";
}
}

?>
<html>
<body>
<form method="get">
Enter Cookie Name:<input type="text"
name="tf1"><br><br>
Enter Cookie Value:<input type="text"
name="tf2"><br><br>
<input type="submit" name="b1" value="Set Cookie">

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

</form>
</body>
</html>

• Delete Cookie:
- To delete cookie, use setCookie() function with an expiration date
in the past.
- Example:
<?php
if(isset($_GET['b1']))
{
$cookie_name=$_GET['tf1'];
$cookie_value=$_GET['tf2'];
setCookie($cookie_name,$cookie_value,time()-
3600);
//deleted cookie as time is in past
if(isset($_COOKIE[$cookie_name]))
{
echo "Cookie set successfully with below details:";
echo "<br>Cookie Name: $cookie_name";
echo "<br>Cookie Value: $cookie_value<br><br>";
}
else
{

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

echo "Cookie is not set";


}
}

?>

<html>
<body>
<form method="get">
Enter Cookie Name:<input type="text"
name="tf1"><br><br>
Enter Cookie Value:<input type="text"
name="tf2"><br><br>
<input type="submit" name="b1" value="Set Cookie">

</form>
</body>
</html>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

====================
***Session****
====================
- Session is a way to store information to be used across multiple
pages.
- Session allow us to store information on web server that associated
session id.
- Once we create session, PHP send cookie that contains session id to
the web browser.
- In the subsequent requests, the web browser send the session id
cookie back to the web server so that PHP can retrive the data based
on session id.
- A session creates file in temporary directory on the server where
registered session variables and their values are stored.
- This data will be available to all pages on the site during that visit.
- The location of temporary file is determined by setting in the
php.ini file called session.save_path

✓ Use of Session:
- Session information is used as medium between two different
entity communication.
- PHP session is used to store data on web server.
- Session Identifier(SID) is a unique number which is used to identify
every user in a session.
- Session is a global variable stored on server.
- Each session has unique id used to retrive stored information.

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

- Whenever session is created, cookie containing session id and it


stored on the user computer. Whenever user send request to server
then it will use session id everytime.
- When we work with an application, we open it, do some changes
and then we close it. This is much like session. The computer knows
who we are. It knows when we start the application and when we
close that application.
- But on the internet, web server dont know who we are or what we
do because HTTP address does not maintain state.
- Session variables solve this problem by storing user information.
Why should session be maintained?
- When there is series of continuous request and response from the
same client to server, the server cannot identify from which cleint it
is getting requests.
- Becase HTTP is a stateless protocol so we need to use session
tracking feature.

✓ Start Session:
- Session is started using method session_start().
- The session_start() function first checks if a session is already
started and if none is started then it starts one.
- Session variables are set with the PHP global variables $_SESSION.
- The $_SESSION[] variables ca be accessed during the lifetime of
session.
- Example:
<?php
if(isset($_GET['b1']))

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

{
session_start();
$_SESSION["favcolor"]="green";
$_SESSION["favanimal"]="dog";

echo "<br>Session Variables are set!!!";

echo "<br><br>Favourite color is


".$_SESSION['favcolor'];
echo "<br>Favourite animal is
".$_SESSION['favanimal'];
}
?>
<html>
<body>
<form method="get">
<input type="submit" name="b1" value="Start Session">
</form>
</body>
</html>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

================
Destroy Session:
=================
- To remove all global session variables and destroy the session, use
session_unset() and session_destroy() functions.

- Example:
<?php
if(isset($_GET['b1']))
{
session_unset();
session_destroy();
}
?>
<html>
<body>
<form method="get">
<input type="submit" name="b1" value="Destroy
Session">
</form>
</body>
</html>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

==================
***Sending Email
==================
- To send email using PHP, we must configure the php.ini file with the
details of how the system send email.
- Open the php.ini file and go the section entitled[Mail function]. Set
the SMTP setting.
- Using mail() function:
- PHP use mail() function to send email.
- This function requires three mandatory arguments that spcify the
recipients of email address, the subject of the message and the
actual message.
- Syntax:
bool mail(String $to,String $subject,String
$message,[String $additional_header,String
$additional_parameters])
- If mail was successfully accedpted for delivery then it return true
value otherwise false.
STEP-1:
- Go to C:/xampp/sendmail anf open sendmail.ini file and make the
below changes.
smtp_server=smtp.gmail.com
smtp_port= 587
smtp_ssl=tls

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

STEP-2:
- Goto C:/xampp/php and open file php.ini and make the below
changes.
smtp=localhost
smtp_port=25
[email protected]
sendmail_path=<path of sendmail.exe file>
- Example:
<?php
function validate_email($msg)
{
$xyz=filter_var($msg,FILTER_SANITIZE_EMAIL);
if(filter_var($msg,FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
$to_email="[email protected]";
$subject="Testing PHP mail";
$message="This mail is sent using PHP mail";

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

//$header="From:[email protected]";

$secure_check=validate_email($to_email);
if($secure_check==false)
{
echo "Invalid Email";
}
else
{
mail($to_email,$subject,$message);
echo "This email is sent using PHP mail";
}
?>

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)


***UNIT-IV Creating and Validating Forms***

Inspiring Your Success

VJTech Academy…

PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)

You might also like