Chapter - 4 Creating and Validating Forms Marks-12: Content Outline
Chapter - 4 Creating and Validating Forms Marks-12: Content Outline
Content outline:
4.1 Creating a webpage using GUI components, browser role-GET and POST methods, Server role
4.2 Form controls: textbox, textarea, radio button, check box, list, and buttons
4.3 Working with multiple forms
4.4 Web page validation
4.5 Cookies: use of cookies, attributes of cookies, create cookies, modify cookies, delete cookies
4.6 Sessions: use of session, start session, get session variables, destroy sesions
4.7 Sending E-mail
4.1 Creating a webpage using GUI components, browser role-GET and POST methods, Server
role
Q. How to handle form in PHP?
Ans:
PHP Code
<?php
echo "Name - ". $_POST["name"];
echo " Email - ". $_POST["email"];
?>
• HTML form consist of text fields, list, buttons etc for getting request from user.
• When user fills the data on the form and submit it, URL is encoded by the browser and
submitted to server for further processing.
Server role:
• When the browser requests a web page from the server, it sets TCP/IP connection and sends
a request in an HTTP format.
• The server verifies the request came from the browser and gives response.
PHP Script
<?php
Notes By- G. R. Jagtap
4
$name = $_GET['uname'];
$pass = $_GET['upass'];
if($name=="admin" && $pass=="123")
echo "Login Successful ";
else
echo "Login Fail";
?>
In above screen, user input is displayed in address bar of the browser. This is because of get()
method.
4.2 Form controls: textbox, text area, radio button, check box, list, and buttons.
a. Textbox / Text field
HTML Code
<html>
<body>
<h3>Form</h3>
<form method = "post" action = "add.php">
Enter Number1:<input type = "text" name = "num1"> <br>
Enter Number2: <input type = "text" name = "num2"> <br>
<input type = "submit" name = "submit" value = "Addition">
</form>
</body>
</html>
PHP Script
<?php
$num1 = $_POST['num1'];
$num2 = $_POST["num2"];
echo "Addition is ".($num1+$num2);
?>
PHP Script
<?php
$addr = $_POST['addr'];
echo "Address is ".$addr;
c. Radio button
HTML Code
<html>
<body>
<form method = "post" action = "add.php">
Select Color:<input type="radio" name="color" value="Red">RED</br>
<input type="radio" name="color" value="Pink">PINK</br>
<input type="radio" name="color" value="Green">GREEN</br><br>
<input type = "submit" name = "submit" value = "Submit">
</form>
</body>
</html>
PHP Script
<?php
$c = $_POST['color'];
echo "Selected Color is ".$c;
d. Check box
HTML Code
<html>
<body>
<form method = "post" action = "add.php">
Select Color:<input type="checkbox" name="c1" value="Red">RED
<input type="checkbox" name="c2" value="Pink">PINK
<input type="checkbox" name="c3" value="Green">GREEN</br>
<br>
<input type = "submit" name = "submit" value = "Submit">
</form>
</body>
</html>
PHP Script
<?php
$c1 = $_POST['c1'];
$c2 = $_POST['c2'];
e. List box
HTML Code
<html>
<body>
<form method="get" action="list.php">
Programming Languages Known: <br>
<select name="lang">
<option value="C">C</option>
<option value="C++">C++</option>
<option value="Java">Java</option>
<option value="PHP">PHP</option>
<option value="Python">Python</option>
</select><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
f. Buttons
HTML Code
<html>
<body>
<form method="get" action="btn.php">
<input type="submit" name="btnSubmit" value="Save">
<input type="submit" name="btnDisc" value="Discard">
</form>
</body>
</html>
PHP Script
Ass1.php
<?php
if(!empty($_POST['button1']))
echo "Name is - ".$_POST['uname'];
?>
Output when “Save Name” button is clicked.
if (empty($name1)) {
$nameErr = "Name is required";
}
if (empty($email1)) {
$emailErr = "Email is required";
}
echo $nameErr." ".$emailErr;
echo "<h4>Your given values are as:</h4>";
echo "Name is -".$name1;
echo "<br>";
echo "Email is -".$email1;
echo "<br>";
echo "Contact number is -".$cnum1;
echo "<br>";
?>
<html>
<body>
<?php
echo "Cookie name- " . $cookie_name."<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
4.6 Sessions: use of session, start session, get session variables, destroy sessions
- An alternative way to make data accessible across the various pages of an entire website is to use
a PHP Session.
- A session creates a file in a 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 the temporary file is determined by a setting in the php.ini file
called session.save_path. Before using any session variable make sure you have setup this path.
- When a PHP script wants to retrieve the value from a session variable, PHP automatically gets
the unique session identifier string from the PHPSESSID cookie and then looks in its temporary
directory for the file bearing that name and a validation can be done by comparing both values.
- A session ends when the user loses the browser or after leaving the site, the server will terminate
the session after a predetermined period of time, commonly 30 minutes duration.
- Starting a PHP Session
- A PHP session is easily started by making a call to the session_start() function.This function first
checks if a session is already started and if none is started then it starts one. It is recommended to
put the call to session_start() at the beginning of the page.
- Session variables are stored in associative array called $_SESSION[]. These variables can be
accessed during lifetime of a session.
- The following example starts a session then register a variable called counter that is incremented
each time the page is visited during the session.
- Make use of isset() function to check if session variable is already set or not.
- Example:
<?php
session_start();
if( isset( $_SESSION['counter'] ) )
{
2)
<?php
session_start();
?>