Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

understanding Common Form Issues(CREATING & USING 3-1

OF FORMS)

When dealing with forms, the most important aspect to remember is


that you are limited to a certain variety of fields that can be applied to a
form.

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-2

registration.html:
<html>
<head>
<title>Registration form</title>
</head>
<body>
<html>
<form action="get-demo.php" method="GET">
<p>GET Example:</p>
User Name: <input type="text" name="uname"
maxlength="150" /><br /><br />
 Password: <input type="password" name="pwd" maxlength="150" /><br />
<input type="submit" value="Submit” />
</form>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-3

GET vs. POST


The two ways available to a web developer that the information
entered into the form is transmitted to its destination by using method.
the two methods are GET and POST.
GET
When sending data using the GET method, all fields are appended to
the Uniform Resource Locator (URL) of the browser and sent along
with the address as data.
Sending data using the GET method means that fields are generally
capped at 150 characters, which is certainly not the most effective
means of passing information.
It is also not a secure means of passing data, because many people
know how to send information to a script using an address bar.
PHP’s current methods for dealing with GET variable is the $_GET
superglobal.
Syntax is $_GET[’Variable Name’];.

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-4

get.php:
<html>
<head>
<title>Example for get</title>
</head>
<body>
<form action="get-demo.php" method="GET">
<p>GET Example:</p>
User Name: <input type="text" name="uname"
maxlength="150" /><br /><br />
 Password: <input type="password" name="pwd" maxlength="150" /><br />
<input type="submit" value="Submit with GET" style="margin-top: 10px;" />
</form>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-5

get-demo.php:
<html>
<head>
<title>Example for GET</title>
</head>
<body>
<?php
if ($_GET['submitted'] == "yes"){
if (trim ($_GET['uname']) != "" && trim ($_GET['pwd']) != ""){ echo "Your User Name
(with GET): " . $_GET['uname'];
echo "<br>Your password (with GET) : ". $_GET['pwd'];
} else {
echo "You must submit a value.";
}
?><br /><a href="get.php">Try Again</a><?php
}
?>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-6

The trim() function is used to remove the white spaces and other
predefined characters from the left and right sides of a string. Specifies
the character(s) to remove. Without this remove the following " " an
ordinary space.

NOTE:
when using the GET method, hitting the Refresh button after
submitting data the browser will automatically send the data
again.

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-7

POST
When sending data using the POST method, values are sent as
standard input (the data will be sended through body not in URL).
Sending data using the POST method is quite a bit more secure .

PHP ’s current methods for dealing with POST variable is the $_POST
superglobal.
Syntax is
$_POST[’Variable Name’];

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-8

Post.php:
<html>
<head>
 <title>Example for POST</title>
</head>
<body>
<form action="post-demo.php" method="post">
<p>POST Example:</p>
<input type="hidden" name="submitted" value="yes" />
User Name: <input type="text" name="uname" maxlength="150" /><br /><br />
Password: <input type="password" name="pwd" maxlength="150" /><br />
<input type="submit" name="submit" value="Submit with POST" style="margin-top:
10px;" />
</form>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-9

Post-demo.php:
<html>
<head>
<title>Example for POST</title>
</head>
<body>
<?php
if(isset($_POST['submitted']))
{
if ($_POST['submitted'] == "yes"){
if (trim ($_POST['uname']) != "" && trim ($_POST['pwd']) != "")
{
echo "Your User Name (with POST): " . $_POST['uname'];
echo "<br>Your password (with POST) : ". $_POST['pwd'];
} else {
echo "You must submit a value.";
}
?><br /><a href="2_get.php">Try Again</a><?php
}
}
?>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
11

VALIDATING FORM INPUT:

Client-side validation: These are the checks that happen in the


browser, before a form is submitted.
The goal here is to make life easier for the people filling out the form.
Examples: HTML5, JavaScript etc.,
Server-side validation: These are the checks that happen after a
form is sent back to the web server.
 At this point, it’s up to your server-side code to review the details and
make sure everything is kosher before continuing. No matter what the
browser does, server side validation is essential.

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
12

The following example shows a few examples of form validation using


PHP.
<html>
<head>
<title>ValidatrionDEMO
</title>
<?php
if($_SERVER["REQUEST_METHOD"]=
="POST"){
if($_POST["uname"]==""){
echo "<font color=red>Please Enter valid User name</font><br>";
}
else if(strlen($_POST["uname"])<6){
echo "<font color=red>Please Enter valid User name with more than 6
chatrecters<br></font>"
}l.

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
13

if($_POST["pwd"]==""){
echo "<font color=red>Please Enter valid Password</font><br>";
}
else if(strlen($_POST["pwd"])<6){
echo "<font color=red>Please Enter valid Password with more than 6
chatrecters<br></font>";
}
}
?>
</head>
<body>
<form method=POST action="<?$_SERVER['PHP_SELF']?>" >
<table>
<tr><td>NAME:</td>
<td><input type=text name="uname" /></td>
</tr>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
14

<tr><td>PASSWORD:</td>
<td><input type=password name="pwd" /></td>
</tr>
<tr><td></td>
<td><input type=hidden name="type" value="Admin" /></td>
</tr>
<tr><td><input type=reset value=CLEAR /></td>
<td><input type=submit value=NEXT /></td>
</tr>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
15

</form>
<?php
if($_SERVER["REQUEST_METHOD"]=
="POST"){
if($_POST["uname"]!="" && strlen($_POST["uname"])>=6 &&
$_POST["pwd"]!="" && strlen($_POST["pwd"])>=6){
echo "Name:<font color=green>".
$_POST['uname']."<br></font>"; echo "Password: <font
color=green>".$_POST['pwd']."<br></font>";
}
}
?>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
16

Working with multiple forms: When each page loads,


you merely load the values from the previous pages into hidden form
elements and submit them
page1.php
<html>
<head>
<title>Personal information</title>
</head>
<body>
<form method=POST action="page2.php">
<input type="text" name="name">
FATHER NAME:input type="text" name="fname"/>
MOTHER NAME:</td><td><input type="text" name="mname"/>
MALE:<input type="radio" name="gen" Value="MALE"/>
FEMALE:<input type="radio" name=gen value=Female/>
<input type=reset value=clear></td>
<input type="submit" value="NEXT>>"></td>
</form></body></html>
Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer
3-
17

page2.php
<html>
<head>
<title>Contact information</title>
</head><body>
<form method=POST action="page3.php"/>
E-Mail:<input type="text" name="email"/>
Mobile:<input type="text" name="Mobile”/>
ADDRESS:</td><td><textarea name=address></textarea></td>
<input type=hidden name="name" value="<?echo $_POST["name"];?>"
/>
<input type=hidden name="fname" value="<?echo
$_POST["fname"];?>" />
<input type=hidden name="mname" value="<?echo
$_POST["mname"];?>" />
<input type=hidden name="gen" value="<?echo $_POST["gen"];?>" />
<tr>).

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
18

<input type=reset value=”clear”/>


<input type="submit" value="NEXT”/>
</form>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
19

<html>
<head>
<title>Complete Information</title>
</head><body>
NAME<?echo $_POST["name"];?>
FATHER NAME</td><td><?echo $_POST["fname"];?>
MOTHER NAME</td><td><?echo $_POST["mname"];?>
GENDER</td><td><?echo $_POST["gen"];?>
E-Mail</td><td><?echo $_POST["email"];?>
<Mobile<?echo $_POST["Mobile"];?>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
20

Preventing Multiple Submissions of a form


Preventing Multiple Submissions on the Server Side:
 If the user attempts to resubmit a request, the script notes a
request is already in motion from this user and denies the subsequent
request
The following script Preventing Multiple Submissions on the Server
Side
<html>
<head>
</head>
<body>
<form name="test" onsubmit="return checkandsubmit ()" method="post"
action="p2.php"> Name::<input type="text" name="uname"><br>
Password::<input type="password" name="pwd"><br>
<input type="submit" value="SUBMIT" id="submitbut"><br>
</form></body></html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
21

p2.php:
<?php
$name=$_POST['uname'];
$pwd=$_POST['pwd'];
session_start(); if(!
isset($_SESSION['x'])){
$_SESSION['x']=TRUE;
f($_SESSION['x']==TR
UE){
mysql_connect("localhost","root","");
mysql_select_db("TEST");
mysql_query("INSERT INTO login('uname','pwd')
VALUES('$name','$pwd')");
$_SESSION['x']=FALSE;
for($i=0;$i<=2000000;$i++);//donothing
for($i=0;$i<=2000000;$i++);//donothing
for($i=0;$i<=2000000;$i++);//donothing

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
22

for($i=0;$i<=2000000;$i+
+);//do
nothing
for($i=0;$i<=2000000;$i++);//do
nothing
}
echo "Successfully added to
database"; session_unset();
?>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
23

Preventing Multiple Submissions on the Client Side:

The following example uses JavaScript to cut off multiple submittals


from a client-side (browser) level.
<html>
<head>
<script>
function checkandsubmit() {
//Disable the submit button.
document.test.submitbut.disabled =
true;
//Then submit the form.
document.test.submit();
}
</script>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer


3-
24

</head>
<body>
<form name="test" onsubmit="return checkandsubmit ()" method="post"
action="p2.php"> Name::<input type="text" name="uname"><br>
Password::<input type="password" name="pwd"><br>
<input type="submit" value="SUBMIT" id="submitbut"><br>
</form>
</body>
</html>

Computer System Architecture Chap. 3 Data Representation Dept. of Info. Of Computer

You might also like