The document discusses HTML forms and handling form data with PHP. It explains how to use the GET and POST methods to send form data to PHP, and how to access submitted form values using $_GET and $_POST superglobals in PHP. It provides examples of handling different form elements like text fields, checkboxes, radio buttons, and select boxes. It also demonstrates how to validate required fields and display error messages.
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
PHP Form Handling Guide
1. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
National Diploma in Information and Communication Technology
PHP :2-FORM-HANDLING>
K72C001M07 - Web Programming
11/23/2018 2-FORM-HANDLING 1
2. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
HTML Form
//login.html
<html>
<body>
<form action="login_get.php" method=“get">
Username: <input type="text" name="userName"><br>
Password: <input type="text" name="password"><br>
<input type="submit">
</form>
</body>
</html>
11/23/2018 2-FORM-HANDLING 2
3. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
When to use GET?
• Information sent from a form with the GET method is visible to
everyone .
• All variable names and values are displayed in the URL.
• GET also has limits on the amount of information to send.
• The limitation is about 2000 characters.
• The variables are displayed in the URL, it is possible to bookmark the
page.
• This can be useful in some cases.
• GET may be used for sending non-sensitive data.
• Note: GET should NEVER be used for sending passwords or other
sensitive information!
11/23/2018 2-FORM-HANDLING 3
4. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
HTTP GET method
//login_get.php
Welcome <?php echo $_GET["userName"]; ?>
<br> Your Password is: <?php echo
$_GET["password"];?>
11/23/2018 2-FORM-HANDLING 4
5. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
When to use POST?
• Information sent from a form with the POST method is invisible to
others.
• All names/values are embedded within the body of the HTTP
request.
• No limits on the amount of information to send.
• Supports advanced functionality such as support for multi-part binary
input while uploading files to server.
• It is not possible to bookmark the page.
• Developers prefer POST for sending form data.
11/23/2018 2-FORM-HANDLING 5
6. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
HTTP POST method
//login_post.php
Welcome <?php echo $_POST["userName"]; ?><br>
Your Password is: <?php echo
$_POST["password"];?>
11/23/2018 2-FORM-HANDLING 6
7. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (1): Form – Sign up
• Write a program to display entered details of following interface
• Method = POST
11/23/2018 2-FORM-HANDLING 7
Last Name
First Name
E-mail
Password
Conform Password
Sign up
8. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (2): Form – Sign in
• Write a program to check user name and password correct or not.
• Give message successfully login or unauthorized access.
• Use your own user name and password
• Login.html
• Login.php
11/23/2018 2-FORM-HANDLING 8
User Name
Password
Sign in
9. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (2): Answer
• Login.html
<html>
<body>
<h1>Login Form</h1>
<form method="POST" action=“Login.php">
<p> Name </p> <input type="text" name="name" size=20
/>
<p> Password </p> <input type="password" name="pass"
size=20 />
<input type="submit" name="login" value="login" />
</form>
</body>
</html
11/23/2018 2-FORM-HANDLING 9
10. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Exercise (2): Answer
• Login.php
<?php
if(($_POST['name']=="user") &&
($_POST['pass']=="pass"))
echo "<h1> Hello ".$_POST['name']."</h1>";
else
echo "<h2> Access Denied </h2>";
?>
11/23/2018 2-FORM-HANDLING 10
11. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
isset( $var)
• Returns TRUE if var exists and has value other than NULL. FALSE
otherwise.
$var = '';
// This will evaluate to TRUE so the text will
be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
11/23/2018 2-FORM-HANDLING 11
12. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Values of Checked Checkboxes
• Example: checkbox.php
<h2>Select your technical exaposer:</h2>
<form action="#" method="post">
<input type="checkbox" name="check_list[]"
value="C/C++"><label>C/C++</label><br/>
<input type="checkbox" name="check_list[]"
value="Java"><label>Java</label><br/>
<input type="checkbox" name="check_list[]"
value="PHP"><label>PHP</label><br/>
<input type="submit" name="submit"
value="Submit"/>
</form>
11/23/2018 2-FORM-HANDLING 12
13. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Values of Checked Checkboxes
• Example: checkbox.php
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['check_list'])){
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
}}}
?>
11/23/2018 2-FORM-HANDLING 13
14. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - single
• Example: select_option.php
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get
Selected Values" />
</form>
11/23/2018 2-FORM-HANDLING 14
15. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - single
• Example: select_option.php
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
echo "You have selected :" . $selected_val;
}
?>
11/23/2018 2-FORM-HANDLING 15
16. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - multiple
• Example: select_option_multiple.php
<form action="#" method="post">
<select name="Color[]" multiple>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get
Selected Values" />
</form>
11/23/2018 2-FORM-HANDLING 16
17. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Get Value of Select Option - multiple
• Example: select_option_multiple.php
<?php
if(isset($_POST['submit'])){
foreach ($_POST['Color'] as $select)
{
echo "You have selected : $select <br/>";
}
}
?>
11/23/2018 2-FORM-HANDLING 17
18. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
• Example: signup.php
<html>
<head>
<title>Sign-Up</title>
<style>
body {
margin: auto;
width: 500px;
}
div {
padding: 10px;
}
div span {
color: red;
}
</style>
</head>
11/23/2018 2-FORM-HANDLING 18
19. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
<body>
<h3>Registration Form</h3>
<?php
$nameErr = $userNameErr = $passwordErr =
$cpasswordErr= "";
$fullname = $email = $userName = $gender =
$password = $cpassword = null;
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
11/23/2018 2-FORM-HANDLING 19
20. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
$nameErr = "Name is required";
} else {
$fullname = $_POST['name'];
}
if (empty($_POST["user"])) {
$userNameErr = "Username is
required";
} else {
$userName = $_POST['user'];
}
11/23/2018 2-FORM-HANDLING 20
21. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
if (empty($_POST["pass"])) {
$passwordErr = "Password is
required";
} else {
$password = $_POST['pass'];
}
$email = $_POST['email'];
$gender = $_POST['gender'];
11/23/2018 2-FORM-HANDLING 21
22. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
$cpassword = $_POST['cpass'];
}
?>
<form method="POST" action="#">
<div>Name<input type="text" name="name"
/><span>*<?php echo $nameErr; ?></span></div>
<div>Email <input type="text"
name="email"></div>
<div>Gender:
<input type="radio" name="gender"
value="Female" checked>Female
<input type="radio" name="gender"
value="Male">Male</div>
11/23/2018 2-FORM-HANDLING 22
23. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
<div>UserName <input type="text"
name="user"><span>*<?php echo $userNameErr;
?></span></div>
<div>Password <input type="password"
name="pass"><span>*<?php echo $passwordErr;
?></span></div>
<div>Confirm Password<input type="password"
name="cpass"></div>
<div><input id="button" type="submit"
name="submit" value="Sign-Up"></div>
</form>
<?php
echo'
<div>Name : '.$fullname.'</div>
11/23/2018 2-FORM-HANDLING 23
24. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Required Fields
<div>Email : '.$email.' </div>
<div>UserName : '.$userName.' </div>
<div>Gender : '.$gender.' </div>
<div>Password : '.$password.' </div>
<div>Confirm Password :
'.$cpassword.'</div>
';
?>
</body>
</html>
11/23/2018 2-FORM-HANDLING 24
25. Sri Lanka-German Training InstituteDepartment of Information and Communication Technology
Reference
www.w3schools.com
www.php.net
Friday, November 23, 2018 25